public class ChannelTest {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("H:/e-touch-etx.zip");
FileChannel inputStreamChannel = inputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("H:/e-touch-bak.zip");
FileChannel channel = fileOutputStream.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
byte[] bytes = new byte[1024];
long l1 = System.currentTimeMillis();
/* while (inputStreamChannel.read(allocate)!= -1){
allocate.flip();
channel.write(allocate);
allocate.clear();
}*/
long l2 = System.currentTimeMillis();
int len = -1 ;
while ((len = inputStream.read(bytes))!= -1){
fileOutputStream.write(bytes,0,len);
}
long l3 = System.currentTimeMillis();
System.out.println("nio 执行耗费时间位 "+(l2-l1));
System.out.println("io 执行耗费时间位 "+(l3-l2));
}
}
nio 使用channel 复制文件
最新推荐文章于 2022-06-19 23:25:27 发布