java8中nio通道(Channel)的基本使用

2025-10-26 19:49:43

1、利用通道完成文件的复制

@Test

    public void test4() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"),

                StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),

                StandardOpenOption.WRITE,

                StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        

        //inChannel.transferTo(0,inChannel.size(),outChannel);

        outChannel.transferFrom(inChannel,0,inChannel.size());

        inChannel.close();

        outChannel.close();

    }

java8中nio通道(Channel)的基本使用

2、使用直接缓冲区完成文件的复制(内存映射文件)

    @Test

    public void test2() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);

        

        //内存映射文件

        MappedByteBuffer inMappedBuff =  inChannel.map(FileChannel.MapMode.READ_ONLY,0,inChannel.size());

        MappedByteBuffer outMappedBuff = outChannel.map(FileChannel.MapMode.READ_WRITE,0,inChannel.size());

        

        //直接对缓冲区进行数据的读写操作

        byte[] dst = new byte[inMappedBuff.limit()];

        

        inMappedBuff.get(dst);

        outMappedBuff.put(dst);

        

    }

java8中nio通道(Channel)的基本使用

3、通道之间的数据传输

 @Test

    public void test4() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"),

                StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),

                StandardOpenOption.WRITE,

                StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        

        inChannel.transferTo(0,inChannel.size(),outChannel);

    }

java8中nio通道(Channel)的基本使用

4、通道之间的数据传输(直接缓冲区)

 @Test

    public void test4() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"),

                StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),

                StandardOpenOption.WRITE,

                StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        

        outChannel.transferFrom(inChannel,0,inChannel.size());

    }

java8中nio通道(Channel)的基本使用

5、关闭输入通道连接

 @Test

    public void test4() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"),

                StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),

                StandardOpenOption.WRITE,

                StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        

        outChannel.transferFrom(inChannel,0,inChannel.size());

        inChannel.close();

    }

java8中nio通道(Channel)的基本使用

6、关闭输出通道连接

@Test

    public void test4() throws Exception{

        FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"),

                StandardOpenOption.READ);

        FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"),

                StandardOpenOption.WRITE,

                StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        

        outChannel.transferFrom(inChannel,0,inChannel.size());

        inChannel.close();

        outChannel.close();

    }

java8中nio通道(Channel)的基本使用

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢