您的位置:首页 > 其它

file.delete()删除不掉文件

2017-06-05 17:28 991 查看
查阅了网上的资料,很多人说是有文件流没有关闭,导致文件被占用,无法删除,但是检查并未发现存在未关闭文件流。

后在网上看到一种解决方式

 public static boolean forceDelete(File f)
{
boolean result = false;
int tryCount = 0;
while(!result && tryCount++ <10)
{
System.gc();
result = f.delete();
}
return result;
} 执行10次垃圾回收后删除,目前可以解决这个问题
继续查阅资料,好像是因为映射占用的问题,在如下网站有解决方式
https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java
public static void unMapBuffer(MappedByteBuffer buffer, Class channelClass) {
if (buffer == null) {
return;
}

try {
Method unmap = channelClass.getDeclaredMethod("unmap", MappedByteBuffer.class);
unmap.setAccessible(true);
unmap.invoke(channelClass, buffer);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐