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. 解除網頁無法選取文字、鎖右鍵限制:Enable Copy(Chrome 擴充套件)

    有些网页因会因为某些因素而禁止浏览者直接复制网页上的内容,虽然我们了解站方的意思,不过有些时候会造成一些不必要的困扰. Enable Copy 这款Chrome 扩充套件可以帮你一键解除封锁右键和选取 ...

  2. Autofac 之 基于 Castle DynamicProxy2 的 Interceptor 功能

    Autofac 结合 Castle DynamicProxy2 功能        Autofac 不仅作为轻量级高效的 IoC 容器,而且还能很好的与 Castle.DynamicProxy2 结合 ...

  3. ubuntu 增加新硬盘

    在校内建了一个ftp,本来是想放一点东西的,没想到越放越多,然后硬盘就不够了. 服务器是虚拟出来的ubuntu-server 12.04.想要将新硬盘挂载到/home/ftp目录. 操作步骤: 1.可 ...

  4. Node.js权威指南 (3) - Node.js基础知识

    3.1 Node.js中的控制台 / 19 3.1.1 console.log方法 / 19 3.1.2 console.error方法 / 20 3.1.3 console.dir方法 / 21 3 ...

  5. web.xml中的contextConfigLocation的作用

    在web.xml中通过contextConfigLocation配置spring,contextConfigLocation 参数定义了要装入的 Spring 配置文件. 如果想装入多个配置文件,可以 ...

  6. Linux下用户及用户组的管理

    一.用户账号管理 1. 添加新用户账号 命令格式: useradd 选项 用户名 选项含义如下: -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果此目录不存在,则同时使用- ...

  7. 折腾iPhone的生活——iOS设备重刷固件

    iOS设备升级系统总共有这么几种方法: 1.OTA升级,也就是我们最常碰到的,在设备上,连上Wifi,在设置里面的软件更新就可以直接通过Wifi安装新的系统(已越狱设备不要这样升级) 2.通过iTun ...

  8. Bzoj 2186: [Sdoi2008]沙拉公主的困惑 乘法逆元,线性筛,欧拉函数,数论

    2186: [Sdoi2008]沙拉公主的困惑 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2560  Solved: 857[Submit][St ...

  9. lvs,haproxy,keepalived,heartbeat

    lvs的是通过vrrp协议进行数据包转发的,提供的是4层的负载均衡.特点是效率高,只要你机器网卡抗的住就不是问题. haproxy可以提供4层或7层的数据转发服务,能做到7层的好处是可以根据服务所处的 ...

  10. github 上的pull request总结

    自己先要fork别人的代码. 然后把代码git clone到本地.. 当自己有提交的时候, 可以在git commit之后 git push origin master 然后这份git push or ...