Assume the file name is in this 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:
import org.apache.commons.io.FileUtils;
FileUtils.copyURLToFile(file, /* some other file */);
Is it possible to validate file existence without exception c开发者_Python百科atching?
You can use the java.util.jar.JarFile class, and use the getJarEntry method to see if the file exists.
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. 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.
精彩评论