zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    《计算机网络 自顶向下方法》 第1章 计算机网络和因特网
    记一次代码优化
    不要刻意寻求局部最优解
    Eclipse Jetty插件安装
    Jetty的工作原理
    log4g 使用教程
    有用资料的网址
    Java 编程 订单、支付、退款、发货、退货等编号主动生成类
    Spring框架
    Eclipse常用快捷键大全1
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/7264039.html
Copyright © 2011-2022 走看看