众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例:
面向字节流的:FileOutputStream 和 BufferedOutputStream
面向字符流的:FileWriter 和 BufferedWriter
近年来发展出New I/O ,也叫NIO,里面又包装了两个类:NewOutputStream 和 NewBufferedWriter
现在,我们建立测试程序,比较这些类写入文件的性能。
机器配置Processor Name: Intel Core i7
Processor Speed: 2.2 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 16 GB
测试程序纵向比较:几种文件操作类向文件中写入相同行数的内容(每行内容均为“写入文件Data\n”),比较其耗费时间
横向比较:对于同一个文件操作类,比较写入不同行数内容情况下所耗费时间;本文以2的次方指数级增长行数
import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class testFileIO {
public static void testDriver () throws IOException {
int maxlineNum = 100000001;//写入文件的最大行数
int startlineNum = 1;//写入文件的行数
int Multiplying = 2;//行数增长倍率
long begin = 0L;
long end = 0L;
//将时间统计写入文件Result.txt中
FileWriter fileWriter = new FileWriter("./Result.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
System.out.println("Test FileOutputStream begin.");
for (int lineNum = startlineNum; lineNum < maxlineNum; lineNum *= Multiplying) {
begin = System.currentTimeMillis();
testFileOutputStream(lineNum);
end = System.currentTimeMillis();
long timeElapse_FileOutputStream = end - begin;
bufferedWriter.write(String.valueOf(timeElapse_FileOutputStream)+"\t");
}
System.out.println("Test FileOutputStream end.\n");
System.out.println("Test BufferedOutputStream begin.");
bufferedWriter.write("\n");
for (int lineNum = startlineNum; lineNum < maxlineNum; lineNum *= Multiplying) {
begin = System.currentTimeMillis();
testBufferedOutputStream(lineNum);
end = System.currentTimeMillis();
long timeElapse_BufferedOutputStream = end - begin;
bufferedWriter.write(String.valueOf(timeElapse_BufferedOutputStream)+"\t");
}
System.out.println("Test BufferedOutputStream end.\n");
System.out.println("Test FileWriter begin.");
bufferedWriter.write("\n");
for (int lineNum = startlineNum; lineNum < maxlineNum; lineNum *= Multiplying) {
begin = System.currentTimeMillis();
testFileWriter(lineNum);
end = System.currentTimeMillis();
long timeElapse_FileWriter = end - begin;
bufferedWriter.write(String.valueOf(timeElapse_FileWriter)+"\t");
}
System.out.println("Test FileWriter end.\n");
System.out.println("Test BufferedWriter begin.");
bufferedWriter.write("\n");
for (int lineNum = startlineNum; lineNum < maxlineNum; lineNum *= Multiplying) {
begin = System.currentTimeMillis();
testBufferedWriter(lineNum);
end = System.currentTimeMillis();
long timeElapse_BufferedWriter = end - begin;
bufferedWriter.write(String.valueOf(timeElapse_BufferedWriter)+"\t");
}
System.out.println("Test BufferedWriter end.\n");
System.out.println("Test NewOutputStream begin.");
bufferedWriter.write("\n");
for (int lineNum = startlineNum; lineNum < maxlineNum; lineNum *= Multiplying) {
begin = System.currentTimeMillis();
testNewOutputStream(lineNum);
end = System.currentTimeMillis();
long timeElapse_NewOutputStream = end - begin;
bufferedWriter.write(String.valueOf(timeElapse_NewOutputStream)+"\t");
}
System.out.println("Test NewOutputStream end.\n");