针对Java的Stream,BufferedStream,NIO,Scanner对文件的io效率做了一个实验,同时结合c为参考,废话不多说,代码如下:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;
class TestIO {
private InputStream ins;
private OutputStream outs;
public TestIO(){}
public TestIO(InputStream ins,OutputStream outs){
this.ins = ins;
this.outs = outs;
}
public void setIns(InputStream is){
this.ins = is;
}
public InputStream getIns(){
return ins;
}
public void setOuts(OutputStream outs){
this.outs = outs;
}
public OutputStream getOuts(){
return outs;
}
}
public class Main {
final static String filename = "D:/testio.txt";
public static void testio(TestIO tio){
byte[] buffer = new byte[4096];
try {
long start = System.currentTimeMillis();
while(tio.getIns().read(buffer)>0){
tio.getOuts().write(buffer);
}
long end = System.currentTimeMillis();
System.out.println("time cost is "+(end-start)+" ms");
tio.getIns().close();
tio.getOuts().close();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
tio.getIns().close();
tio.getOuts().close();
} catch(IOException e){
e.printStackTrace();
}
}
}
public static void createInputFile(){
try{
OutputStream os = new BufferedOutputStream(new FileOutputStream(filename));
for(int i=0;i<50000000;++i){
os.write("A B C D E F G\n".getBytes());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
// createInputFile();
TestIO tio;
try{
//buffered stream
tio = new TestIO(new FileInputStream(filename),new FileOutputStream(filename+".bak1"));
tio.setIns(new BufferedInputStream(tio.getIns(),8192));
tio.setOuts(new BufferedOutputStream(tio.getOuts(),8192));
System.out.println("buffered stream method :");
testio(tio);
//stream
tio = new TestIO(new FileInputStream(filename),new FileOutputStream(filename+".bak2"));
System.out.println("normal stream method :");
testio(tio);
//nio
long s = System.currentTimeMillis();
FileChannel fci = new FileInputStream(filename).getChannel();
FileChannel fco = new FileOutputStream(filename+".bak3").getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(8192);
while(fci.read(bb) != -1){
bb.flip();
fco.write(bb);
bb.clear();
}
long e = System.currentTimeMillis();
System.out.println("\nnio : "+(e-s)+" ms");
//scanner
s = System.currentTimeMillis();
Scanner in = new Scanner(new FileInputStream(filename));
while(in.hasNextLine()){
in.nextLine();
}
e = System.currentTimeMillis();
System.out.println("\nScanner : "+(e-s)+" ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
c:
#include<time.h>
#include<stdio.h>
main()
{
long s = clock();
FILE* fpr = fopen("D:/testio.txt","r");
FILE* fpw = fopen("D:/testio.txt.bak_c","w");
char buff[8192];
while(fgets(buff,sizeof buff,fpr)!=NULL){
fputs(buff,fpw);
}
long e = clock();
printf("%f\n",1.0*(e-s)/CLOCKS_PER_SEC);
}
运行结果不贴了,有兴趣的话可以跑跑看,这里只把结论说一下。
Java的BufferedStream, Stream, NIO的速度差别很小,都与C一样快;但是JAVA在用着三种方式IO的时候CPU占用率极低,而C几乎独占了一个CPU。
Java的Scanner就没什么可说的了,超级慢,而且也超级消耗CPU资源。
看来前面的那个测试对Java有些误解,sorry啦。