Java-NIO(五):通道(Channel)的数据传输与内存映射文件
2017-07-31 16:52
537 查看
[b]通道(Channel)的数据传输(采用非直接缓冲区)[/b]
[b] 内存映射文件(采用直接缓冲区)[/b]
transferTo&transferFrom将数据从源通道传输到其他 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(); }
[b] 内存映射文件(采用直接缓冲区)[/b]
/** * 内存映射文件 * * @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)的数据传输与内存映射文件
- NIO技术-3-内存映射文件及文件通道到通道批量传输数据
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
- JAVA NIO FileChannel 内存映射文件
- 黑马程序员_O‘Reilly java nio学习笔记之通道_内存映射文件&&Channel-to-Channel 传输
- Java NIO笔记(六):内存映射文件及文件通道到通道批量传输数据
- 08. Java NIO FileChannel 文件通道
- [Java][IO]JAVA NIO之浅谈内存映射文件原理与DirectMemory
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- Java NIO 通道之间的数据传输
- JAVA-4NIO之Channel之间的数据传输方法
- Java NIO笔记之内存映射文件
- Java NIO之内存映射文件——MappedByteBuffer
- Java NIO 内存映射文件实现进程间通信
- java nio 映射文件内容到内存
- 12. Java NIO DatagramChannel 数据报通道
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- JAVA NIO 内存映射文件原理与DirectMemory