直接上结论:
- BufferedOutputStream直接写byte[]最快;比BufferedWriter快一倍;
- FileOutputStream最慢,差数量级;
- 从快到慢顺序:BufferedOutputStream直接写byte[]>BufferedWriter>FileWrite>BufferedOutputStream>FileOutputStream直接写byte[]>FileOutputStream;
- GetBytes相当耗时,在有Buffer的流时,比写入文件还耗时。
- 本次测试环境:笔记本 HP ZBOOK 15
- 本次测试共写入100万行,写入到SSD盘
- 每行内容:ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789
程序输出记录如下:
Test Performance Version 0.2
DESC:Test FileOutputStream、BufferedOutputStream、FileWriter、BufferedWriter、GetBytes.
Author: Charlie Chen
Date: 2021.01.03
------------------------ Info --------------------
Line content:ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789
Lint count: 1000000
------------------------ Info --------------------
------------------------ Begin --------------------
FileOutputStream Elapse: 3388 ms
FileOutputStreamBytes Elapse: 2944 ms
BufferedOutputStream Elapse: 298 ms
BufferedOutputStreamBytes Elapse: 101 ms
FileWriter Elapse: 279 ms
BufferedWriter Elapse: 213 ms
GetBytes Elapse: 187 ms
------------------------ End --------------------
程序源码如下:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
//测试FileOutputStream、BufferedOutputStream、FileWriter、BufferedWriter、GetBytes的性能
public class Test {
// 写文件行数
private static int count = 1000000;
// 写文件每行的内容
private static String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789\r\n";
// 写文件每行的内容,byte数组格式
private static byte[] contentBytes = content.getBytes();
//主程序
public static void main(String[] args) throws Exception {
System.out.println("Test Performance Version 0.2");
System.out.println("DESC:Test FileOutputStream、BufferedOutputStream、FileWriter、BufferedWriter、GetBytes.");
System.out.println("Author: Charlie Chen");
System.out.println("Date: 2021.01.03");
System.out.println("");
System.out.println("------------------------ Info --------------------");
System.out.println("Line content:"+content.substring(0,content.length()-2));
System.out.println("Lint count: " + count);
System.out.println("------------------------ Info --------------------");
System.out.println("");
System.out.println("------------------------ Begin --------------------");
testFileOutputStream();
testFileOutputStreamBytes();
testBufferedOutputStream();
testBufferedOutputStreamBytes();
testFileWriter();
testBufferedWriter();
testGetBytes();
System.out.println("------------------------ End --------------------");
}
// 测试FileOutputStream的性能,并输出
private static void testFileOutputStream() throws Exception {
// 测试FileOutputStream
FileOutputStream os = null;
try {
// 打开文件
os = new FileOutputStream(new File("C:\\temp\\FileOutputStream.txt"));
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
os.write(content.getBytes());
}
os.flush();
os.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("FileOutputStream Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
os.close();
}
}
// 测试FileOutputStream的性能(直接写byte数组),并输出
private static void testFileOutputStreamBytes() throws Exception {
// 测试FileOutputStream
FileOutputStream os = null;
try {
// 打开文件
os = new FileOutputStream(new File("C:\\temp\\FileOutputStreamBytes.txt"));
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
os.write(contentBytes);
}
os.flush();
os.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("FileOutputStreamBytes Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
os.close();
}
}
// 测试BufferedOutputStream的性能,并输出
private static void testBufferedOutputStream() throws Exception {
// 测试BufferedOutputStream
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
// 打开文件
fos = new FileOutputStream(new File("C:\\temp\\BufferedOutputStream.txt"));
bos = new BufferedOutputStream(fos);
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
bos.write(content.getBytes());
}
bos.flush();
bos.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("BufferedOutputStream Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
bos.close();
}
}
// 测试BufferedOutputStream的性能(直接写byte数组),并输出
private static void testBufferedOutputStreamBytes() throws Exception {
// 测试BufferedOutputStream
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
// 打开文件
fos = new FileOutputStream(new File("C:\\temp\\BufferedOutputStreamBytes.txt"));
bos = new BufferedOutputStream(fos);
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
bos.write(contentBytes);
}
bos.flush();
bos.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("BufferedOutputStreamBytes Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
bos.close();
}
}
// 测试FileWriter的性能,并输出
private static void testFileWriter() throws Exception {
// 测试FileWriter
FileWriter fw = null;
try {
// 打开文件
fw = new FileWriter("C:\\temp\\FileWriter.txt");
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
fw.write(content);
}
fw.flush();
fw.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("FileWriter Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
fw.close();
}
}
// 测试BufferedWriter的性能,并输出
private static void testBufferedWriter() throws Exception {
// 测试BufferedWriter
FileWriter fw = null;
BufferedWriter bfw = null;
try {
// 打开文件
fw = new FileWriter("C:\\temp\\BufferedWriter.txt");
bfw = new BufferedWriter(fw);
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
bfw.write(content);
}
bfw.flush();
bfw.close();
long end = System.currentTimeMillis();
// 打印输出
System.out.println(formatStr("BufferedWriter Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
bfw.close();
}
}
// 测试GetBytes的性能,并输出
private static void testGetBytes() throws Exception {
try {
long begin = System.currentTimeMillis();
// 写入数据
for (int i = 0; i < count; i++) {
content.getBytes();
}
long end = System.currentTimeMillis();
// 打印输出
System.out.println("");
System.out.println( formatStr("GetBytes Elapse:") + (end - begin) + " ms");
} catch (Exception e) {
e.printStackTrace();
}
}
// 把字符串变成长度40,不足补空格
private static String formatStr(String s) {
int len=40;
String rs="";
for(int i=0;i<len;i++) {
if(i<s.length()) {
rs+=s.charAt(i);
}
else {
rs+=" ";
}
}
return rs;
}
}