Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26175516
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2011-01-03 11:58:20

    /**
     * @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;
        }
        
    }


阅读(1251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~