[英]How to check existence of a JAR file and a specific file inside JAR?
Assume the file name is in this URL: 假设文件名在此URL中:
URL file = new URL("jar:file:/path/to/foo.jar!/META-INF/file.txt");
This operation leads to IOException
if there is no foo.jar
or there is no file.txt
inside the existing jar: 如果没有foo.jar
或者现有jar中没有file.txt
,则此操作会导致IOException
:
import org.apache.commons.io.FileUtils;
FileUtils.copyURLToFile(file, /* some other file */);
Is it possible to validate file existence without exception catching? 是否可以验证文件存在而无异常捕获?
You can use the java.util.jar.JarFile class , and use the getJarEntry method to see if the file exists. 您可以使用java.util.jar.JarFile类 ,并使用getJarEntry方法查看该文件是否存在。
JarFile jar = new JarFile("foo.jar");
JarEntry entry = jar.getJarEntry("META-INF/file.txt");
if (entry != null) {
// META-INF/file.txt exists in foo.jar
}
You can examine the CLASSPATH and search for the jar yourself. 您可以检查CLASSPATH并自己搜索jar。 Then you can inspect the jar yourself. 然后你可以自己检查一下罐子。 I guess it will not be faster than your way, nor will it be more easy to understand, so why do you want to avoid the Exception? 我想它不会比你的方式更快,也不会更容易理解,所以你为什么要避免异常呢?
I don't see something wrong in using the exception. 我没有看到使用异常的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.