使用BufferedWriter和FileWriter向文件写字符,并将异常转换为Runtim...

代码模板以下,防止重复劳动,关键点是: html

1.使用BufferedWriter封装了FileWriter,使用了装饰者模式,每次write()方法的调用,并无真正的写数据,而是等到写满8192个char的时候,调用flushBuffer()才真正的写字符到硬盘,或调用close()方法时,触发flushBuffer()一次。 java

2异常捕获,转换为运行时异常,这样在后面的代码调用过程当中,不须要try-catch,直接抛出运行时异常。 性能

若是系统任何IO异常都是致命的异常,那么就能够先try-catch IO异常,而后转化为RuntimeException,之后有其余方法调用这个写文件的方法: this

好处: spa

a.不须要try-catch,(try-catch自己占用系统一点点性能,到是可有可无) 日志

b.遇到IO异常,就抛出RunTimeException,整个系统中止运行,防止遇到错误,程序还空跑。 code

try-catch性能分析: htm

http://www.cnblogs.com/isline/archive/2010/04/22/1717837.html blog


/**
     * 做用:向一个文件内输入信息
     * 
     * @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 的代码以下: ip


/**
 * 做用:对异常进行包装,使之变成不须要捕获的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;
    }

}
相关文章
相关标签/搜索