在写文件的时候,大家可能都不会考虑到性能的问题,只要实现了性能就可以了,实际上带缓冲的输出流在性能上更佳!!!
如下例子:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
/**
* @编写人: yh.zeng
* @编写时间:2017-1-23 下午5:57:55
* @文件描述: todo
*/
public class IOTest {
public static void main(String args[]) throws Exception{
long beginTime = System.currentTimeMillis();
Writer writer01 = new FileWriter("E:\\test.txt");
for(int i =0; i < 100000; i++){
writer01.write(i);
}
writer01.close();
System.out.println("不带缓冲的输出流耗时:"+ (System.currentTimeMillis() - beginTime));
new File("E:\\test.txt").delete();
beginTime = System.currentTimeMillis();
Writer writer02 = new BufferedWriter( new FileWriter("E:\\test.txt") );
for(int i =0; i < 100000; i++){
writer