Java常用函数封装[原创]

1277阅读 0评论2011-01-03 hkebao
分类:Java

    /**
     * @author a
     * @see 写文件
     * @param String fileContent
     * @param String filePath
     * @param int type
     * type 1 :使用FileWriter的方式
     *         2:使用FileOutputStream
     *         3:BufferedOutputStream
     * */

    private void writeFile(String fileContent,String filePath,int type) {
        FileWriter fw = null;
        FileOutputStream out = null;
        FileOutputStream outSTr = null;
        BufferedOutputStream Buff=null;
        switch (type) {
        case 1:
            try {
                 fw = new FileWriter(filePath);//"C:/add2.txt"

                 fw.write(fileContent);
            } catch (Exception e) {
     e.printStackTrace();
     } finally {
         try {
     fw.close();
     } catch (Exception e) {
     e.printStackTrace();
     }
     }
            break;
        case 2:
            try {
                 outSTr = new FileOutputStream(new File(filePath));
     Buff=new BufferedOutputStream(outSTr);
                 Buff.write(fileContent.getBytes());
                 Buff.flush();
         Buff.close();
            } catch (Exception e) {
     e.printStackTrace();
     } finally {
         try {
             outSTr.close();
     } catch (Exception e) {
     e.printStackTrace();
     }
     }
        case 3:
            try {
                 out = new FileOutputStream(new File(filePath));
                 out.write(fileContent.getBytes());
                 out.close();
            } catch (Exception e) {
     e.printStackTrace();
     } finally {
         try {
             out.close();
     } catch (Exception e) {
     e.printStackTrace();
     }
     }
        default:
            break;
        }
        
    }


上一篇:python代码整理
下一篇:Red Hat Linux 安装 GCC[4.6虚拟机安装GCC]