Java-NIO(五):通道(Channel)的数据传输与内存映射文件
2017-12-20 15:29
681 查看
[b]通道(Channel)的数据传输(采用非直接缓冲区)[/b]
1
@Test
[b] 内存映射文件(采用直接缓冲区)[/b]
1
/**
transferTo&transferFrom将数据从源通道传输到其他
Channel 中(采用直接缓存区)
1
public
void testTransfer()
throws IOException {
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 }
[b] 内存映射文件(采用直接缓冲区)[/b]
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 }
相关文章推荐
- 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