java nio file 内存映射文件
来源:互联网 发布:淘宝 一件代发 编辑:程序博客网 时间:2024/06/05 12:44
原理:内存映射文件原理探索
java对象解析:java内存映射文件
java对象中,介绍是用普通的bytebuffer来做的,而我们在实际生产环境中使用MappedByteBuffer。
介绍中是操作FileChannel的write、force、close等方法。使用MappedByteBuffer = FileChannel.map(..)产生,只要操作MappedByteBuffer的put、force方法就好了
MappedByteBuffer代码:
package file.ram.map;import sun.misc.Cleaner;import sun.nio.ch.DirectBuffer;import sun.nio.ch.FileChannelImpl;import java.io.File;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.StandardCopyOption;import java.nio.file.StandardOpenOption;import java.util.concurrent.atomic.AtomicInteger;public class FileMMap { private static AtomicInteger pos = new AtomicInteger(); private static FileChannel channel; private static MappedByteBuffer mmap; private static long size = 1024L; public static void main(String[] args) throws IOException, InterruptedException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { File file = new File("./test.dump"); channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); mmap = channel.map(FileChannel.MapMode.READ_WRITE, 0, size); byte[] len = new byte[8]; mmap.get(len); System.out.println(mmap.capacity()); System.out.println(mmap.limit()); System.out.println(mmap.remaining()); mmap.put("abc".getBytes(StandardCharsets.UTF_8)); // channel.close(); // System.out.println(mmap.slice()); for (int i = 0; i < 10000; i++) {// channel.transferTo(0, channel.size(), FileChannel.open(new File("./test1.dump").toPath(),// StandardOpenOption.CREATE, StandardOpenOption.WRITE)); // 移动 Files.move(file.toPath(), new File("./test1.dump").toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); channel.close(); // 新建 channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); // 手动unmap// Method m = FileChannelImpl.class.getDeclaredMethod("unmap",// MappedByteBuffer.class);// m.setAccessible(true);// m.invoke(FileChannelImpl.class, mmap); // 可以直接释放MappedByteBuffer Cleaner var1 = ((DirectBuffer)mmap).cleaner(); if(var1 != null) { var1.clean(); } mmap = channel.map(FileChannel.MapMode.READ_WRITE, 0, size); mmap.put(String.valueOf(i + 1).getBytes(StandardCharsets.UTF_8)); } }}
阅读全文
0 0
- java nio file 内存映射文件
- java nio 内存映射文件
- Java NIO 内存映射文件
- Java nio 之 内存映射文件
- JAVA NIO 内存映射文件 与DirectMemory
- Java--文件内存映射--NIO--MapedByteBuffer
- NIO - 内存映射文件
- java.nio 映射文件
- Java NIO 文件IO-内存映射文件MappedByteBuffer与zerocopy
- java nio 之MappedByteBuffer,高效文件/内存映射
- java nio 之MappedByteBuffer,高效文件/内存映射
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- java nio 之MappedByteBuffer,高效文件/内存映射
- JAVA NIO 内存映射文件原理与DirectMemory
- java nio 之MappedByteBuffer,高效文件/内存映射
- Java NIO 内存映射文件实现进程间通信
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- 解决win 7不是正版
- leetcode 299. Bulls and Cows
- 第二周函数参数传递的三种方式
- 二叉搜索树代码
- [LeetCode]15. 3Sum
- java nio file 内存映射文件
- 最新阿里大于短信验证码node koa2的实现
- 627. Swap Salary
- JAVA设计模式之单例模式
- 第二周 函数参数传递的三种方式
- 一次Mysql死锁排查过程的全纪录
- 漏洞挖掘-混浠的代码
- Tomcat 怎么停止服务的?
- Enable exfat format on Ubuntu 16.04