关于MappedByteBuffer使用后无法删除问题
来源:互联网 发布:edf调度算法源代码 编辑:程序博客网 时间:2024/06/06 16:30
网上已经提供了些方法,我只是为了做个备份资料 . . .
以下就是我测试用的代码了
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.lang.reflect.Method;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.security.AccessController;import java.security.PrivilegedAction;import org.junit.Assert;import org.junit.Before;import org.junit.Test;public class TestMappedByteBuffer {File file;MappedByteBuffer buff;/** * initialization process * * @throws Exception */@Beforepublic void init() throws Exception {file = new File("/tmp/test.txt");if (!file.exists()) {file.createNewFile();}FileOutputStream out = new FileOutputStream(file);out.write("TEST\nTEST".getBytes());out.close();}/** * delete file method * * @throws Exception */@Testpublic void deleteFile() throws Exception {mappedFile();cleanHandle(buff);file.delete();// check existsAssert.assertEquals(false, file.exists());}/** * Mapped file * * @throws Exception */public void mappedFile() throws Exception {FileChannel channel = new FileInputStream(file).getChannel();buff = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());channel.close();}/** * clean handle method * * @param obj * process object */public void cleanHandle(final Object obj) {// validate method parameterif (obj == null) {return;}AccessController.doPrivileged(new PrivilegedAction<Object>() {public Object run() {try {Method cleaner = obj.getClass().getMethod("cleaner",new Class[0]);if (cleaner != null) {cleaner.setAccessible(true);// 1.method((sun.misc.Cleaner) cleaner.invoke(obj, new Object[0])).clean();// 2.method/*Object __obj = cleaner.invoke(obj, new Object[0]);Method clean = __obj.getClass().getMethod("clean",new Class[0]);if (clean != null) {clean.invoke(__obj, new Object[0]);}*/}} catch (Exception e) {e.printStackTrace();}return null;}});}}
0 0
- 关于MappedByteBuffer使用后无法删除问题
- 关于MappedByteBuffer资源释放问题
- 关于win7升级win10后vs无法使用的问题
- 关于图片在窗体显示后,无法更新或删除的问题
- 关于金山软件杀毒软件卸载后Kingsoft大文件无法删除的问题
- 关于jdk安装后手动删除文件夹后无法再次安装的问题。
- 关于Cygwin无法删除的问题
- 关于无法删除Windows目录的问题
- 关于ubuntu用户无法删除问题
- 关于CodeBlocks安装后无法编译问题
- 关于使用Struts2使用result-dispatcher后,无法找到JSP内css、js路径的问题
- 关于使用科学上网后,win10系统步无法使用ie和edge的问题
- lua关于编译后无法使用
- 关于Xcode升级后插件无法使用
- 关于AVD无法使用问题
- MappedByteBuffer的使用
- MappedByteBuffer使用注意点
- MappedByteBuffer的使用
- 汽车百科系列之(二): 大话机油
- linux_C_environ变量
- Visual Studio2012 Lua插件--BabeLua
- jm8.6之参考帧管理
- 打印一个N*N的方阵,N为每边字符的个数( 3〈N〈20 ),写出来真是泪牛满面啊。
- 关于MappedByteBuffer使用后无法删除问题
- 汽车百科系列之(三): 轮胎无小事
- C++成员函数强制转换,适应C风格!
- bat-best-cmd
- error C2018: unknown character '0xa1'这个错误是什么意
- linux (none)login和init prefdm respawning too fast,stopped的解决办法(非重新装系统)
- Linux 2.6内核配置说明 (国嵌)
- 汽车百科系列之(四): 四驱究竟是什么意思
- WebRTC学习笔记_Demo收集