Java-NIO(五):通道(Channel)的数据传输与内存映射文件

  • 通道(Channel)的数据传输(采用非直接缓冲区)
1 @Test
 2     public void testChannel() throws IOException {
 3         FileInputStream fileInputStream = new FileInputStream("Java NIO.pdf");
 4         FileOutputStream fileOutputStream = new FileOutputStream("2.pdf");
 5 
 6         // 1、获取通道
 7         FileChannel inChannel = fileInputStream.getChannel();
 8         FileChannel outChannel = fileOutputStream.getChannel();
 9 
10         // 2.分配指定大小的缓冲区
11         ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
12 
13         // 3、将通道的数据读入缓冲区
14         while (inChannel.read(byteBuffer) != -1) {
15             byteBuffer.flip();// 切换缓冲区为读模式
16             // 4、把缓冲区的数据写入通道
17             outChannel.write(byteBuffer);
18             byteBuffer.clear();// 因为需要循环多次读,需要清空缓冲区。
19         }
20 
21         byteBuffer.clear();
22         inChannel.close();
23         outChannel.close();
24         fileInputStream.close();
25         fileOutputStream.close();
26     }

  •  内存映射文件(采用直接缓冲区) 
1 /**
 2      * 内存映射文件
 3      * 
 4      * @throws IOException
 5      */
 6     @Test
 7     public void testMemoryMappingFile() throws IOException {
 8         long start = System.currentTimeMillis();
 9         
10         FileChannel inChannel = FileChannel.open(Paths.get("D:\\nio.zip"), StandardOpenOption.READ);
11         // 注意:StandardOpenOption.CREATE
12         // 如果文件已经存在,直接覆盖,StandardOpenOption.CREATE_NEW,如果文件已经存在,就抛出异常。
13         FileChannel outChannel = FileChannel.open(Paths.get("E:\\nio.zip"), StandardOpenOption.READ,
14                 StandardOpenOption.WRITE, StandardOpenOption.CREATE);
15 
16         // 获取内存映射文件
17         MappedByteBuffer inMappedByteBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
18         MappedByteBuffer outMappedByteBuffer = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
19 
20         // 直接对数据进行读写 
21         byte[] bytes = new byte[inMappedByteBuffer.limit()];
           // 此时,如果数据读超出了一定返回会抛出异常。如果内存不足时,会抛出java.lang.OutOfMemoryError: Java heap space
22         inMappedByteBuffer.get(bytes);
23         outMappedByteBuffer.put(bytes);
24 
25         inChannel.close();
26         outChannel.close();
27         long end = System.currentTimeMillis();
28 
29         System.out.println((end - start));
30     }

  • transferTo&transferFrom将数据从源通道传输到其他 Channel 中(采用直接缓存区)
1 public void testTransfer() throws IOException {
 2         FileChannel inChannel = FileChannel.open(Paths.get("D:\\nio.zip"), StandardOpenOption.READ);
 3         // 注意:StandardOpenOption.CREATE
 4         // 如果文件已经存在,直接覆盖,StandardOpenOption.CREATE_NEW,如果文件已经存在,就抛出异常。
 5         FileChannel outChannel = FileChannel.open(Paths.get("E:\\nio.zip"), StandardOpenOption.READ,
 6                 StandardOpenOption.WRITE, StandardOpenOption.CREATE);
 7 
 8         //inChannel.transferTo(0, inChannel.size(), outChannel);
 9         outChannel.transferFrom(inChannel, 0, inChannel.size());
10     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值