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.
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 写入文件.的更多相关文章
- java写入文件的几种方法分享
转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...
- java写入文件的几种方法小结
一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...
- 运用BufferedWriter把数据写入文件
public class WriteReadFiles { private static Logger log = LoggerFactory.getLogger(WriteReadFiles.cla ...
- Java将字符串写入文件与将文件内容读取到字符串
原文:http://blog.csdn.net/liuweiyuxiang/article/details/69487326 将字符串写入文件 方法一 public void WriteStringT ...
- [测试]java IO写入文件效率——几种方法比较
各类写入方法 /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void outpu ...
- java将错误信息写入文件
第一种办法可以通过字符串,也就是先把错误信息写入字符串,再将字符串写入文件 import java.io.*; public class Demo { public static void main( ...
- 两个Map的对比,三种方法,将对比结果写入文件。
三种方法的思维都是遍历一个map的Key,然后2个Map分别取这2个Key值所得到的Value. #第一种用entry private void compareMap(Map<String, S ...
- java读取写入文件
先来看一下java写入文件 public static void readTxt(String value) throws IOException { FileWriter fw = null; tr ...
- Android将Log写入文件
为什么要将Log写入文件 运行应用程序的时候,大多数是不会连接着IDE的: 而当应用程序崩溃时,我们需要收集复现步骤,在设备上复现,并进行Debug: 而由于Android手机的多样性,有些问题是某个 ...
随机推荐
- 我所理解的OOP——UML六种关系(转)
转自:http://www.cnblogs.com/dolphinX/p/3296681.html 最近由于经常给公司的小伙伴儿们讲一些OOP的基本东西,每次草纸都被我弄的很尴尬,画来画去自己都乱了, ...
- Delphi中代替WebBrowser控件的第三方控件
这几天,接触到在delphi中内嵌网页,用delphi7自带的TWebBrowser控件,显示的内容与本机IE8显示的不一样,但是跟装IE8之前的IE6显示一个效果.现在赶脚是下面两个原因中的一个: ...
- 【POJ】1056 IMMEDIATE DECODABILITY
字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...
- error: /lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference
]$ sudo yum install libnotify*Loaded plugins: fastestmirror, refresh-packagekit, securitySetting up ...
- HDU-4974 A simple water problem
http://acm.hdu.edu.cn/showproblem.php?pid=4974 话说是签到题,我也不懂什么是签到题. A simple water problem Time Limit: ...
- Unity3d 真实的植物渲染
好久没写shader了,有些生疏,刚弄了个植物shader,分享一下. 先上图片: 重点需要注意的是fragment shader的透明部分 需要如此声明 Tags{ "LightMode& ...
- 通过命令名称查询进程id
linux 中如何通过命令名称查询出进程的id呢? 例如,我想查询java的进程id: ps -ef |grep java |grep -v grep|awk '{print $2}' 或者: ps ...
- .Net remoting 的文件传输
http://www.codeproject.com/Articles/14100/Dot-Net-Remoting-Handling-Remote-Events-using-Dele
- Java编程-基本语句
一个基本的输出语句: package Hello; import java.util.Scanner; public class Hello { public static void main(Str ...
- hdoj 2141 Can you find it?【二分查找+暴力】
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...