代码模板如下,防止重复劳动,关键点是:
1.使用BufferedWriter封装了FileWriter,使用了装饰者模式,每次write()方法的调用,并没有真正的写数据,而是等到写满8192个char的时候,调用flushBuffer()才真正的写字符到硬盘,或调用close()方法时,触发flushBuffer()一次。
2异常捕获,转换为运行时异常,这样在后面的代码调用过程中,不需要try-catch,直接抛出运行时异常。
如果系统任何IO异常都是致命的异常,那么就可以先try-catch IO异常,然后转化为RuntimeException,以后有其他方法调用这个写文件的方法:
好处:
a.不需要try-catch,(try-catch本身占用系统一点点性能,到是无关紧要)
b.遇到IO异常,就抛出RunTimeException,整个系统停止运行,防止遇到错误,程序还空跑。
try-catch性能分析:
http://www.cnblogs.com/isline/archive/2010/04/22/1717837.html
/**
* 作用:向一个文件内输入信息
*
* @param strFileName
* 文件名
* @param strContent
* 输入的内容
* @param isAppend
* 是否追加在前一文件之后
*/
public static void createFileBuffer(String strFileName, String strContent, boolean isAppend)
{
// 建立父级文件
mkParentPath(strFileName);
// 进行写操作
BufferedWriter writer = null;
try
{
writer = new BufferedWriter(new FileWriter(strFileName, isAppend));
writer.write(strContent);
writer.flush();
} catch (IOException ex)
{
throw ExceptionUtil.wrap("createFile:write file error." + strFileName, ex);
} finally
{
close(writer);
}
}
/**
* 作用:为父级文件建立目录
*
* @param strPath
*/
public static void mkParentPath(String strPath)
{
File file = new File(strPath);
mkParentPath(file);
}
public static void mkParentPath(File file)
{
File parent = file.getParentFile();
if (parent != null)
if (!parent.exists())
parent.mkdirs();
}
public static void close(Closeable closeable)
{
try
{
if (closeable != null)
closeable.close();
} catch (IOException ex)
{
}
}
ExceptionUtil 的代码如下:
/**
* 作用:对异常进行包装,使之变成不需要捕获的RuntimeException
*/
public final class ExceptionUtil {
private ExceptionUtil() {
}
/**
* 作用:包装异常,使之变成不需要catch的RuntimeException
* @param ex 所要包装的异常
* @return 返回SysException
*/
public static SysException wrap(Throwable ex) {
SysException se = null;
if (ex instanceof SysException) {
se = (SysException)ex;
}
else {
se = new SysException(ex);
}
ex.printStackTrace();
return se;
}
/**
* 作用:包装异常,使之变成不需要catch的RuntimeException
* @param message 异常信息
* @param ex 所要包装的异常
* @return 返回SysException
*/
public static SysException wrap(String message, Throwable ex) {
SysException se = null;
if (ex instanceof SysException) {
se = (SysException)ex;
}
else {
se = new SysException(message, ex);
}
return se;
}
/**
* 作用:包装异常,使之变成不需要catch的RuntimeException
* @param msgId 异常消息id
* @param ex 所要包装的异常
* @return 返回SysException
*/
public static SysException wrap(long msgId, Throwable ex) {
SysException se = null;
if (ex instanceof SysException) {
se = (SysException)ex;
}
else {
se = new SysException(msgId, ex);
}
return se;
}
/**
* 作用:包装异常,使之变成不需要catch的RuntimeException
* @param ex 所要包装的异常
* @param logged 是否需要记录log日志
* @return 返回SysException
*/
public static SysException wrap(Throwable ex, boolean logged) {
SysException se = wrap(ex);
se.setLogged(logged);
return se;
}
/**
* 作用:包装异常,使之变成不需要catch的RuntimeException
* @param message 异常信息
* @param ex 所要包装的异常
* @param logged 是否需要记录log信息
* @return 返回SysException
*/
public static SysException wrap(String message, Throwable ex, boolean logged) {
SysException se = wrap(message, ex);
se.setLogged(logged);
return se;
}
}
SysException 的代码如下:
/**
* 作用:一个不需要catch的RuntimeException
* @classname SysException.java
* @description 系统异常类
*/
public class SysException extends RuntimeException implements Serializable
{
private Long msgId;
private boolean isLogged;
public SysException()
{
super();
this.isLogged = false;
}
public SysException(String msg)
{
super(msg);
this.isLogged = false;
}
public SysException(String msg, Throwable cause)
{
super(msg, cause);
this.isLogged = false;
}
public SysException(Throwable cause)
{
super();
this.isLogged = false;
}
public SysException(long messageId)
{
this();
this.msgId = new Long(messageId);
}
public SysException(long messageId, Throwable cause)
{
this(cause);
this.msgId = new Long(messageId);
}
public boolean isLogged()
{
return isLogged;
}
public void setLogged(boolean b)
{
isLogged = b;
}
public Long getMsgId()
{
return msgId;
}
}