Java写入文件的几种方法及性能对比

 1 package demo;
 2 import java.io.File;   
 3 import java.io.FileOutputStream;   
 4 import java.io.*;   
 5 
 6 public class FileOperate {   
 7 
 8     public static void main(String[] args) {   
 9 
10         FileOutputStream out = null;   
11         FileOutputStream outSTr = null;   
12         BufferedOutputStream Buff=null;   
13         FileWriter fw = null;   
14         int count=50000;//写文件行数   
15 
16         try {   
17             //****************************FileOutputStream保存**************************************
18             out = new FileOutputStream(new File("E:/add0.html"));   
19             long begin = System.currentTimeMillis();   
20             for (int i = 0; i < count; i++) {   
21                 out.write("测试java 文件操作".getBytes());   
22             }   
23             out.close();   
24             long end = System.currentTimeMillis();   
25             System.out.println("FileOutputStream执行耗时:" + (end - begin) + " 豪秒");   
26             
27             //****************************BufferedOutputStream保存***********************************
28             outSTr = new FileOutputStream(new File("E:/add1.html"));   
29              Buff=new BufferedOutputStream(outSTr);   
30             long begin0 = System.currentTimeMillis();   
31             for (int i = 0; i < count; i++) {   
32                 Buff.write("测试java 文件操作".getBytes());   
33             }   
34             Buff.flush();   
35             Buff.close();   
36             long end0 = System.currentTimeMillis();   
37             System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 豪秒");   
38 
39             //****************************fileWriter保存*********************************************
40             fw = new FileWriter("E:/add2.html");   
41             long begin3 = System.currentTimeMillis();   
42             for (int i = 0; i < count; i++) {   
43                 fw.write("测试java 文件操作");   
44             }   
45             fw.close();   
46             long end3 = System.currentTimeMillis();   
47             System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒");   
48             
49             //****************************outPutStreamWriter保存**************************************
50             FileOutputStream os = new FileOutputStream(new File("E:/add3.html"));
51             OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF-8");
52             long begin4 = System.currentTimeMillis();  
53             for (int i = 0; i < count; i++) {
54                 osWriter.write("测试java 文件操作");
55             }
56             osWriter.close();
57             long end4 = System.currentTimeMillis();   
58             os.close();
59             System.out.println("outPutStreamWriter执行耗时:" + (end4 - begin4) + " 豪秒");  
60             
61             FileWriter fileWritter = new FileWriter(new File("E:/add4.html"));
62             BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
63             long begin5 = System.currentTimeMillis();
64             for (int i = 0; i < count; i++) {
65             bufferWritter.write("测试java 文件操作");
66             }
67             bufferWritter.close();
68             long end5 = System.currentTimeMillis();   
69             System.out.println("BufferedWriter执行耗时:" + (end5 - begin5) + " 豪秒");
70         } catch (Exception e) {   
71             e.printStackTrace();   
72         } finally {   
73             try {   
74                 fw.close();   
75                 Buff.close();   
76                 outSTr.close();   
77                 out.close();   
78             } catch (Exception e) {   
79                 e.printStackTrace();   
80             }   
81         }   
82     }   
83 
84 }

 

转载于:https://www.cnblogs.com/wanying521/p/5179256.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值