PrintWriter 和 BufferedWriter 写入文件.

Ref: should I use PrintWriter to wrap BufferedWriter?

 

The main reason for using PrintWriter is the wealth of convenience functions which do intelligent output of various things. If you don't need that capability, you wouldn't use a PrintWriter at all.The point of a BufferedWriter is to allow output to be buffered and then written in a batch.If you batch I/O to and from external devices, you can really reduce the overall cost because output (or input) can be batched--and you end up waiting for completion just once per batch, rather than once per I/O operation. 

 1 import java.io.PrintWriter;
 2 import java.io.BufferedWriter;
 3 import java.io.IOException;
 4 import java.io.FileWriter;
 5 import java.io.File;
 6 class Printer {
 7     public static void write(String path, String contents) throws IOException {
 8         PrintWriter pw = new PrintWriter(new FileWriter(new File(path)));
 9         pw.print(contents);
10         pw.close();
11     }
12 }
13 
14 class Buffered {
15     public static void write(String path, String contents) throws IOException {
16         BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
17         bw.write(contents);
18         bw.close();
19     }
20 }

User.java 文件是测试类,将内容写入文件中,

 1 import java.io.IOException;
 2 class User{
 3     public static void main(String[] args){
 4         StringBuffer sb = new StringBuffer();
 5         for(int i = 0; i < 100000; i++)
 6         sb.append("telerikkjuatlistenedk76914761ilovemyprofessionaljob.").append("\n");
 7         try{
 8             long startTime = System.nanoTime();
 9 //            Printer.write("H:\\OwnTool\\WriterDataToText\\out.txt",sb.toString());
10             Buffered.write("H:\\OwnTool\\WriterDataToText\\out.txt",sb.toString());
11             long endTime = System.nanoTime();
12             System.out.println("cost '" + (endTime - startTime) + "' ns ");
13         }catch(IOException e){
14             e.printStackTrace();
15         }
16     }
17 }

多次试验都得到差不多的结果,BufferedWriter花的时间会比 PrintWriter '少'很多:

对于 BufferedWriter 的输出结果:

cost '72761343' ns

对于 PrintWriter 的输出结果:

cost '95209656' ns

 

 


 

 1 /*
 2 author:kju
 3 create a class that write string content to a specified path.
 4 */
 5 package kju.write;
 6 
 7 import java.io.PrintWriter;
 8 import java.io.IOException;
 9 import java.io.BufferedWriter;
10 import java.io.FileWriter;
11 import kju.print.Printer;
12 public class Writer
13 {
14     public static void PrintWrite(String path, String content) throws IOException {
15         PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path)));
16         pw.write(content);
17         pw.close();
18     }    
19     public static void BufferedWrite(String path, String content) throws IOException {
20         BufferedWriter bw = new BufferedWriter(new FileWriter(path));
21         bw.write(content);
22         bw.close();
23     }    
24     public static void main(String[] args) 
25     {
26         String s = "";
27         String path = "D:\\out.txt";
28         for (int i = 0; i < 10000; i++)
29         {
30             s += "kjuatlistened";
31         }
32         try
33         {
34             long start = System.nanoTime();
35             //PrintWrite(path,s);
36             BufferedWrite(path,s);
37             long end = System.nanoTime();
38             Printer.println("done,elapse " + (end - start) + " s");
39         }
40         catch (IOException ex)
41         {
42             ex.printStackTrace();
43         }
44     }
45 }

 

the comparation is result shown with a screenshot:

 

 

转载于:https://www.cnblogs.com/listened/p/3526099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值