/**
* 测试NIO文件传输
* @throws IOException
*/
@Test
public void transferFileByNIO() throws IOException {
long begian = System.currentTimeMillis();
//创建流
FileInputStream inputStream=new FileInputStream("D:\\data\\files\\windows_7_ultimate_x64_2018.iso");
FileOutputStream outputStream=new FileOutputStream("D:\\data\\files\\windows_7_ultimate_x64_2018-copy.iso");
//获取Channel
FileChannel channelIn=inputStream.getChannel();
FileChannel channelOut = outputStream.getChannel();
//创建Buffer 1M
ByteBuffer buffer = ByteBuffer.allocate(1024*1024);
//读入
int i=0;
while (channelIn.read(buffer)!=-1){
//重设Buffer
buffer.flip();
//写出
i+= channelOut.write(buffer);
//清空Buffer
buffer.clear();
}
inputStream.close();
outputStream.close();
long end = System.currentTimeMillis();
System.out.println("一共传送数据:"+i/1000+"KB");
System.out.println("传输文件所用时间:"+(end-begian)+"sm");
}