MappedByteBuffer
作者:互联网
MappedByteBuffer是java nio引入的文件内存映射方案,读写性能极高。在NIO中主要用到普通的输入流,带缓冲的输入流,RandomAccessFile和MappedByteBuffer。
@Test public void mmapTest() throws Exception { FileChannel fileChannel = null; try { String filePath = "D:\\temp\\avatar.jpg"; fileChannel = new RandomAccessFile(new File(filePath), "rw").getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size()); byte[] bytes = new byte[(int) fileChannel.size()]; mappedByteBuffer.get(bytes); print(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (fileChannel != null) { try { fileChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void print(byte[] bytes) { for (int i = 0; i < bytes.length; i++) { System.out.printf("0x%02X ", bytes[i]); if ((i + 1) % 8 == 0 || i + 1 == bytes.length) { System.out.print("\n"); } } }
标签:bytes,MappedByteBuffer,fileChannel,print,new,byte 来源: https://www.cnblogs.com/vipsoft/p/16458161.html