JDK1.4中引入的NIO库,和传统IO最大的区别是:传统IO是基于Byte(字节)和Stream(流)的,而NIO是基于Buffer(缓冲)、Channel(通道)和Selector(选择器)的。下面就看下使用IO,BufferedIO和NIO分别实现的文件复制(本来想测试一下分别的性能,但是结果不是很稳定,希望有这方面数据的同学分享下):
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileUtil {
public static void main(String args[]){
String srcFilePath = "E:/solr-4.1.0.zip";
String destFilePath = "E:/0.zip";
String destFilePath1 = "E:/1.zip";
String destFilePath2 = "E:/2.zip";
File srcFile = new File(srcFilePath);
File destFile = new File(destFilePath);
File destFile1 = new File(destFilePath1);
File destFile2 = new File(destFilePath2);
long start = System.currentTimeMillis();
for(int i=0;i<10;i++){
copyFileByNIO(srcFile, new File("E:/"+i+".zip"));
}
long end = System.currentTimeMillis();
System.out.println("All times="+(end-start));
/*copyFileByNIO(srcFile, destFile2);
copyFileByIO(srcFile, destFile);
copyFileByBufferedIO(srcFile, destFile1);*/
}
public static void copyFileByNIO(File srcFile, File destFile){
long start = System.currentTimeMillis();
FileInputStream in = null;
FileOutputStream out = null;
FileChannel fcin = null;
FileChannel fcout = null;
try{
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
fcin = in.getChannel();
fcout = out.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(fcin.read(buffer)!=-1){
buffer.flip();
fcout.write(buffer);
buffer.clear();
}
}catch(IOException e){
e.printStackTrace();
}finally{
close(in);
close(out);
close(fcin);
close(fcout);
long end = System.currentTimeMillis();
System.out.println("copyFileByNIO: time="+(end-start));
}
}
public static void copyFileByIO(File srcFile, File destFile){
long start = System.currentTimeMillis();
FileInputStream is = null;
FileOutputStream os = null;
try{
is =new FileInputStream(srcFile);
os = new FileOutputStream(destFile);
byte[] bytes = new byte[1024];
int count=0;
while((count=is.read(bytes)) != -1){
os.write(bytes, 0, count);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
close(is);
close(os);
long end = System.currentTimeMillis();
System.out.println("copyFileByIO: time="+(end-start));
}
}
public static void copyFileByBufferedIO(File srcFile, File destFile){
long start = System.currentTimeMillis();
BufferedInputStream is = null;
BufferedOutputStream os = null;
try{
is =new BufferedInputStream(new FileInputStream(srcFile));
os = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int count=0;
while((count=is.read(bytes)) != -1){
os.write(bytes, 0, count);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
close(is);
close(os);
long end = System.currentTimeMillis();
System.out.println("copyFileByBufferedIO: time="+(end-start));
}
}
public static void close(Closeable device){
if(device != null){
try{
if(device instanceof Flushable){
((Flushable) device).flush();
}
device.close();
}catch(IOException e){
}
}
}
private FileUtil(){}
}