Java的canread结果为false_java - file.delete()返回false,即使file.exists(),file.canRead(),file.canWrite(),file...

另一种可能发生的极端情况:如果您通过URL读取/写入JAR文件,稍后尝试在同一JVM会话中删除相同的文件。

File f = new File("/tmp/foo.jar");

URL j = f.toURI().toURL();

URL u = new URL("jar:" + j + "!/META-INF/MANIFEST.MF");

URLConnection c = u.openConnection();

// open a Jar entry in auto-closing manner

try (InputStream i = c.getInputStream()) {

// just read some stuff; for demonstration purposes only

byte[] first16 = new byte[16];

i.read(first16);

System.out.println(new String(first16));

}

// ...

// i is now closed, so we should be good to delete the jar; but...

System.out.println(f.delete()); // says false!

原因是Java的内部JAR文件处理逻辑,倾向于缓存JarFile条目:

// inner class of `JarURLConnection` that wraps the actual stream returned by `getInputStream()`

class JarURLInputStream extends FilterInputStream {

JarURLInputStream(InputStream var2) {

super(var2);

}

public void close() throws IOException {

try {

super.close();

} finally {

// if `getUseCaches()` is set, `jarFile` won't get closed!

if (!JarURLConnection.this.getUseCaches()) {

JarURLConnection.this.jarFile.close();

}

}

}

}

每个JarFile (相反,底层的ZipFile结构)都会保存文件的句柄,从构造开始直到调用close() :

public ZipFile(File file, int mode, Charset charset) throws IOException {

// ...

jzfile = open(name, mode, file.lastModified(), usemmap);

// ...

}

// ...

private static native long open(String name, int mode, long lastModified,

boolean usemmap) throws IOException;

显然有两种方法来“修复”这个:

您可以禁用JAR文件缓存 - 对于当前的URLConnection ,或者当前JVM会话中的所有未来URLConnection (全局):URL u = new URL("jar:" + j + "!/META-INF/MANIFEST.MF"); URLConnection c = u.openConnection(); // for only c c.setUseCaches(false); // globally; for some reason this method is not static, // so we still need to access it through a URLConnection instance :( c.setDefaultUseCaches(false);

[HACK WARNING!]完成后,您可以从缓存中手动清除JarFile 。 缓存管理器sun.net.www.protocol.jar.JarFileFactory是包私有的,但是一些反射魔法可以为你完成工作:class JarBridge { static void closeJar(URL url) throws Exception { // JarFileFactory jarFactory = JarFileFactory.getInstance(); Class> jarFactoryClazz = Class.forName("sun.net.www.protocol.jar.JarFileFactory"); Method getInstance = jarFactoryClazz.getMethod("getInstance"); getInstance.setAccessible(true); Object jarFactory = getInstance.invoke(jarFactoryClazz); // JarFile jarFile = jarFactory.get(url); Method get = jarFactoryClazz.getMethod("get", URL.class); get.setAccessible(true); Object jarFile = get.invoke(jarFactory, url); // jarFactory.close(jarFile); Method close = jarFactoryClazz.getMethod("close", JarFile.class); close.setAccessible(true); //noinspection JavaReflectionInvocation close.invoke(jarFactory, jarFile); // jarFile.close(); ((JarFile) jarFile).close(); } } // and in your code: // i is now closed, so we should be good to delete the jar JarBridge.closeJar(j); System.out.println(f.delete()); // says true, phew.

请注意:所有这些都基于Java 8代码库( 1.8.0_144 ); 它们可能无法与其他/更高版本一起使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值