我使用nio和io来测试一个100m的文件,虽然每次速度不同,但是都是io的比nio的快怎么回事。但还是nio 快!!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class CopyFile {
/**
* nio拷贝
* @param inFile 源文件
* @param outFile 目标文件
* @return
* @throws Exception
*/
public static long FileChannelCopy(String inFile,String outFile) throws Exception
{
long begin = System.currentTimeMillis();
File in = new File(inFile);
File out = new File(outFile);
FileInputStream fin = new FileInputStream(in);
FileOutputStream fout = new FileOutputStream(out);
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
int bufferLen = 2097152;
ByteBuffer bb = ByteBuffer.allocateDirect(bufferLen);
while (true)
{
int ret = inc.read(bb);
if (ret == -1)
{
fin.close();
fout.flush();
fout.close();
break;
}
bb.flip();
outc.write(bb);
bb.clear();
}
long end = System.currentTimeMillis();
long runtime = 0;
if(end > begin)
runtime = end - begin;
return runtime;
}
/**
* io拷贝
* @param inFile 源文件
* @param outFile 目标文件
* @return
* @throws Exception
*/
public static long FileStraeamCopy(String inFile,String outFile) throws Exception
{
long begin = System.currentTimeMillis();
File in = new File(inFile);
File out = new File(outFile);
FileInputStream fin=new FileInputStream(in);
FileOutputStream fout=new FileOutputStream(out);
int length=2097152;//2m内存
byte[] buffer=new byte[length];
while(true)
{
int ins=fin.read(buffer);
if(ins==-1)
{
fin.close();
fout.flush();
fout.close();
break;
}else
fout.write(buffer,0,ins);
}
long end = System.currentTimeMillis();
long runtime = 0;
if(end > begin)
runtime = end - begin;
return runtime;
}
static public void main(String args[]) throws Exception {
String inFile = "D://big4.pdf"; //源文件
String outFile = "D://big4copy1.pdf"; //输出文件1
String outFile2 = "D://big4copy2.pdf"; //输出文件2
long runtime1,runtime2;
runtime1 = FileChannelCopy(inFile,outFile);
runtime2 = FileStraeamCopy(inFile,outFile2);
System.out.println("FileChannelCopy running time:" + runtime1);
System.out.println("FileStraeamCopy running time:" + runtime2);
}
}
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
//从得到的耗时结果来看 nio效率高很多!!
public class TestFileChannel {
/*
public static void main(String args[])throws Exception{
ByteBuffer buff=ByteBuffer.allocate(1024);
FileInputStream fileIn=new FileInputStream("d:/JavaTests/TestFileChannel.txt");
FileOutputStream fileOut=new FileOutputStream("d:/JavaTests/TestFileChannelOut.txt");
FileChannel in=fileIn.getChannel();
FileChannel out=fileOut.getChannel();
long l=System.currentTimeMillis();
while(in.read(buff)!=-1){
buff.flip(); //把极限设为位置 再把位置设为0
out.write(buff);
buff.clear(); //不改变极限 把位置设为0
}
System.out.println("Take time:"+(System.currentTimeMillis()-l)+"ms");//31ms
in.close();
out.close();
fileIn.close();
fileOut.close();
} */
public static void main(String[] args)throws Exception{
FileInputStream fileIn=new FileInputStream("d:/JavaTests/TestFileChannel.txt");
FileOutputStream fileOut=new FileOutputStream("d:/JavaTests/TestFileOutputStreamCopyTime.txt");
int i;
long l=System.currentTimeMillis();
while((i=fileIn.read())!=-1){
fileOut.write(i);
}
System.out.println("Take time:"+(System.currentTimeMillis()-l)+"ms");//516ms
fileIn.close();
fileOut.close();
}
}
还可以用FileChannel的transferTo(long position, long count, WritableByteChannel target)方法试试!!!