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手机的多样性,有些问题是某个 ...
随机推荐
- 如何得到UBUNTU源代码
http://www.pleaseguide.me/367/how-to-get-the-source-code-of-ubuntu 在按书作测试,有难点一一解决. Ubuntu's Source c ...
- 通过使用CyclicBarrier来计算Matrix中最大的值
import java.util.Random; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.Exec ...
- TEdit,TMemo背景透明
The component below works perfectly, except for the following problem: 1) Saves the component below ...
- SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表)
原文:SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表) 作为SQL Server 2016(CTP3.x)的另一 ...
- wcf中的File-less Activation
File-less Activation Although .svc files make it easy to expose WCF services, an even easier approac ...
- 深入浅出Node.js (附录B) - 调试Node
B.1 Debugger B.2 Node Inspector B.2.1 安装Node Inspector B.2.2 错误堆栈
- 去掉eclipse js 错误提示
1.去掉项目目录底下的.project文件中的以下部分:<buildCommand> <name>org.eclipse.wst.jsdt.core.javascri ...
- maya 操作自我整理(一)
绘制曲线时的点的控制 当我们在使用CV Curve Tool或者EP Curve Tool创建NURBS曲线的过程中,按下"Insert"键,配合键盘上的上.下箭头方向键,可以自由 ...
- JavaScript高级程序设计18.pdf
系统对话框 alert().confirm()和prompt()调用系统对话框向用户显示消息,显示对话框的时候代码会停止执行,关闭后继续执行 alert()警告提示框 confirm()点击确认返回t ...
- C#中同时使用Lambda表达式和递归
Func<object, int> RTFunc = (RTFuncRT) => { return (RTFuncRT as Func<object, int>)(RTF ...