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

  • 通道(Channel)的数据传输(采用非直接缓冲区)
     @Test
public void testChannel() throws IOException {
FileInputStream fileInputStream = new FileInputStream("Java NIO.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("2.pdf"); // 1、获取通道
FileChannel inChannel = fileInputStream.getChannel();
FileChannel outChannel = fileOutputStream.getChannel(); // 2.分配指定大小的缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024); // 3、将通道的数据读入缓冲区
while (inChannel.read(byteBuffer) != -1) {
byteBuffer.flip();// 切换缓冲区为读模式
// 4、把缓冲区的数据写入通道
outChannel.write(byteBuffer);
byteBuffer.clear();// 因为需要循环多次读,需要清空缓冲区。
} byteBuffer.clear();
inChannel.close();
outChannel.close();
fileInputStream.close();
fileOutputStream.close();
}
  •  内存映射文件(采用直接缓冲区)
     /**
* 内存映射文件
*
* @throws IOException
*/
@Test
public void testMemoryMappingFile() throws IOException {
long start = System.currentTimeMillis(); FileChannel inChannel = FileChannel.open(Paths.get("D:\\nio.zip"), StandardOpenOption.READ);
// 注意:StandardOpenOption.CREATE
// 如果文件已经存在,直接覆盖,StandardOpenOption.CREATE_NEW,如果文件已经存在,就抛出异常。
FileChannel outChannel = FileChannel.open(Paths.get("E:\\nio.zip"), StandardOpenOption.READ,
StandardOpenOption.WRITE, StandardOpenOption.CREATE); // 获取内存映射文件
MappedByteBuffer inMappedByteBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMappedByteBuffer = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size()); // 直接对数据进行读写
byte[] bytes = new byte[inMappedByteBuffer.limit()];
// 此时,如果数据读超出了一定返回会抛出异常。如果内存不足时,会抛出java.lang.OutOfMemoryError: Java heap space
inMappedByteBuffer.get(bytes);
outMappedByteBuffer.put(bytes); inChannel.close();
outChannel.close();
long end = System.currentTimeMillis(); System.out.println((end - start));
}
  • transferTo&transferFrom将数据从源通道传输到其他 Channel 中(采用直接缓存区)
     public void testTransfer() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("D:\\nio.zip"), StandardOpenOption.READ);
// 注意:StandardOpenOption.CREATE
// 如果文件已经存在,直接覆盖,StandardOpenOption.CREATE_NEW,如果文件已经存在,就抛出异常。
FileChannel outChannel = FileChannel.open(Paths.get("E:\\nio.zip"), StandardOpenOption.READ,
StandardOpenOption.WRITE, StandardOpenOption.CREATE); //inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
}
上一篇:Java-NIO(四):通道(Channel)的原理与获取


下一篇:TCP/IP、UDP、HTTP、SOCKET详解