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.

 import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileWriter;
import java.io.File;
class Printer {
public static void write(String path, String contents) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter(new File(path)));
pw.print(contents);
pw.close();
}
} class Buffered {
public static void write(String path, String contents) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
bw.write(contents);
bw.close();
}
}

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

 import java.io.IOException;
class User{
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
for(int i = ; i < ; i++)
sb.append("telerikkjuatlistenedk76914761ilovemyprofessionaljob.").append("\n");
try{
long startTime = System.nanoTime();
// Printer.write("H:\\OwnTool\\WriterDataToText\\out.txt",sb.toString());
Buffered.write("H:\\OwnTool\\WriterDataToText\\out.txt",sb.toString());
long endTime = System.nanoTime();
System.out.println("cost '" + (endTime - startTime) + "' ns ");
}catch(IOException e){
e.printStackTrace();
}
}
}

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

对于 BufferedWriter 的输出结果:

cost '72761343' ns

对于 PrintWriter 的输出结果:

cost '95209656' ns


 /*
author:kju
create a class that write string content to a specified path.
*/
package kju.write; import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import kju.print.Printer;
public class Writer
{
public static void PrintWrite(String path, String content) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path)));
pw.write(content);
pw.close();
}
public static void BufferedWrite(String path, String content) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
bw.write(content);
bw.close();
}
public static void main(String[] args)
{
String s = "";
String path = "D:\\out.txt";
for (int i = 0; i < 10000; i++)
{
s += "kjuatlistened";
}
try
{
long start = System.nanoTime();
//PrintWrite(path,s);
BufferedWrite(path,s);
long end = System.nanoTime();
Printer.println("done,elapse " + (end - start) + " s");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}

the comparation is result shown with a screenshot:

PrintWriter 和 BufferedWriter 写入文件.的更多相关文章

  1. java写入文件的几种方法分享

    转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...

  2. java写入文件的几种方法小结

    一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...

  3. 运用BufferedWriter把数据写入文件

    public class WriteReadFiles { private static Logger log = LoggerFactory.getLogger(WriteReadFiles.cla ...

  4. Java将字符串写入文件与将文件内容读取到字符串

    原文:http://blog.csdn.net/liuweiyuxiang/article/details/69487326 将字符串写入文件 方法一 public void WriteStringT ...

  5. [测试]java IO写入文件效率——几种方法比较

    各类写入方法 /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void outpu ...

  6. java将错误信息写入文件

    第一种办法可以通过字符串,也就是先把错误信息写入字符串,再将字符串写入文件 import java.io.*; public class Demo { public static void main( ...

  7. 两个Map的对比,三种方法,将对比结果写入文件。

    三种方法的思维都是遍历一个map的Key,然后2个Map分别取这2个Key值所得到的Value. #第一种用entry private void compareMap(Map<String, S ...

  8. java读取写入文件

    先来看一下java写入文件 public static void readTxt(String value) throws IOException { FileWriter fw = null; tr ...

  9. Android将Log写入文件

    为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...

随机推荐

  1. PHP日常杂记

    1.php点击按钮跳转页面 <input type="button" onclick="window.location.href='login.php'" ...

  2. 优雅的让Fragment监听返回键

    转载请注明出处:http://write.blog.csdn.net/postedit/40507387 Activity可以很容易的得到物理返回键的监听事件,而Fragment却不能.假设Fragm ...

  3. 【HDOJ】3584 Cube

    三位树状数组. /* 3584 */ #include <iostream> #include <string> #include <map> #include & ...

  4. MySQL数据表修复, 如何修复MySQL数据库(MyISAM / InnoDB)

    常用的Mysql数据库修复方法有下面3种: 1. mysql原生SQL命令: repair 即执行REPAIR TABLE SQL语句 语法:REPAIR TABLE tablename[,table ...

  5. (转载)浅谈javascript的分号

    (转载)http://www.blueidea.com/tech/web/2009/7261.asp javascript的分号代表语句的结束符,但由于javascript具有分号自动插入规则,所以它 ...

  6. expect远程登陆hw交换机--测试通过1

    #!/bin/bash #connect_the_hw_switch #by osx1260@.com swip=172.16.56.2 swport= swpasswd='yourswpasswd' ...

  7. OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(三)——计算节点的安装

    序:OpenStack Havana 部署在Ubuntu 12.04 Server [OVS+GRE] 计算节点: 1.准备结点 安装好ubuntu 12.04 Server 64bits后,进入ro ...

  8. Matlab编程-图形处理功能

    绘图功能最基本的命令行:plot(y). 二维图形: (1) >> y=rand(100,1); >> plot(y) y是随机的实向量,以生成y的索引为横坐标,y为纵坐标绘图 ...

  9. An Easy Problem?! - POJ 2826(求面积)

    题目大意:有两块木板交叉起来接雨水,问最多能接多少.   分析:题目描述很简单,不过有些细节还是需要注意到,如下图几种情况:   #include<stdio.h> #include< ...

  10. poj 1321 棋盘问题【dfs】

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28308   Accepted: 13996 Descriptio ...