一、本文讀寫的內容爲簡單的字符串,主要比較了BufferedWriter(BufferedReader)、BufferedOutputStream(BufferedInputStream)、ByteBuffer(write&read)和MappedByteBuffer(write&read)的讀寫速度差異。測試時都是先寫入文件5000000個字符串“Java is one of the best computer languages!\n”,然後再將所有數據讀取到內存中。程序分別記錄了上述四種方法的讀寫速度。
測試程序爲:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class IOStudy {
public static void main(String[] args) {
test1();//BufferedWriter(BufferedReader)
test2();//BufferedOutputStream(BufferedInputStream)
test3();//ByteBuffer(write&read)
test4();//MappedByteBuffer(write&read)
}
public static void test1() {
BufferedWriter bw = null;
BufferedReader br = null;
long time;
try {
time = System.currentTimeMillis();
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(".//data//test1.txt"), "utf-8"));
for(int i = 0; i < 5000000; i++) {
bw.write("Java is one of the best computer languages!\n");
}
bw.flush();
System.out.println("BufferedWriter: " + (System.currentTimeMillis() - time) + "ms");
time = System.currentTimeMillis();
br = new BufferedReader(new InputStreamReader(new FileInputStream(".//data//test1.txt"), "utf-8"));
//要慢一點605ms,但是使用比較方便
// String inputline = br.readLine();
// while(inputline != null) {
// inputline = br.readLine();
// }
//要快一點
char[] data = new char[5000000 * 44];
br.read(data);
System.out.println("BufferedReader: " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bw != null) bw.close();
if(br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test2() {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
long time;
try {
time = System.currentTimeMillis();
bos = new BufferedOutputStream(new FileOutputStream(".//data//test2.txt"));
for(int i = 0; i < 5000000; i++) {
bos.write("Java is one of the best computer languages!\n".getBytes());
}
bos.flush();
System.out.println("BufferedOutputStream: " + (System.currentTimeMillis() - time) + "ms");
time = System.currentTimeMillis();
bis = new BufferedInputStream(new FileInputStream(".//data//test2.txt"));
byte[] data = new byte[5000000 * 44];
bis.read(data);
System.out.println("BufferedInputStream: " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bos != null) bos.close();
if(bis != null) bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test3() {
FileOutputStream fos = null;
FileChannel fc1 = null;
FileInputStream fis = null;
FileChannel fc2 = null;
long time;
try {
time = System.currentTimeMillis();
fos = new FileOutputStream(".//data//test3.txt");
fc1 = fos.getChannel();
ByteBuffer b = ByteBuffer.wrap("Java is one of the best computer languages!\n".getBytes());
for(int i = 0; i < 5000000; i++) {
fc1.write(b);
b.rewind();
}
System.out.println("ByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");
time = System.currentTimeMillis();
fis = new FileInputStream(".//data//test3.txt");
fc2 = fis.getChannel();
ByteBuffer data = ByteBuffer.allocate(5000000 * 44);
fc2.read(data);
System.out.println("ByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fc1 != null) fc1.close();
if(fc2 != null) fc1.close();
if(fos != null) fos.close();
if(fis != null) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test4() {
FileChannel fc1 = null;
FileChannel fc2 = null;
long time;
try {
time = System.currentTimeMillis();
fc1 = new RandomAccessFile(".//data//test4.txt", "rw").getChannel();
ByteBuffer b = fc1.map(FileChannel.MapMode.READ_WRITE, 0, 5000000 * 44);
for(int i = 0; i < 5000000; i++) {
b.put("Java is one of the best computer languages!\n".getBytes());
}
System.out.println("MappedByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");
time = System.currentTimeMillis();
fc2 = new FileInputStream(".//data//test4.txt").getChannel();
b = fc2.map(FileChannel.MapMode.READ_ONLY, 0, fc2.size());
byte[] data = new byte[5000000 * 44];
b.get(data);
System.out.println("MappedByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fc1 != null) fc1.close();
if(fc2 != null) fc2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
測試結果爲:
可以看出來,BufferedWriter的寫速度是最快的,MappedByteBuffer的讀速度是最快的,但是一般BufferedReader操作方便,並且其速度也不是特別慢。所以一般情況下建議字符串讀寫用BufferedWriter&BufferedReader;對於全文讀速度要求高的話可以用MappedByteBuffer。