nio 使用channel 复制文件

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));
    }
}

版权声明:本文为m0_37958655原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。