Java资源作为文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java resource as file
提问by Mantrum
Is there a way in Java to construct a File instance on a resource retrieved from a jar through the classloader?
Java 中有没有办法在通过类加载器从 jar 检索的资源上构造 File 实例?
My application uses some files from the jar (default) or from a filesystem directory specified at runtime (user input). I'm looking for a consistent way of
a) loading these files as a stream
b) listing the files in the user-defined directory or the directory in the jar respectively
我的应用程序使用 jar(默认)或运行时指定的文件系统目录(用户输入)中的一些文件。我正在寻找一种一致的方法
a) 将这些文件作为流加载
b) 分别列出用户定义目录或 jar 中的目录中的文件
Edit: Apparently, the ideal approach would be to stay away from java.io.File altogether. Is there a way to load a directory from the classpath and list its contents (files/entities contained in it)?
编辑:显然,理想的方法是完全远离 java.io.File。有没有办法从类路径加载目录并列出其内容(其中包含的文件/实体)?
采纳答案by Jon Skeet
ClassLoader.getResourceAsStream
and Class.getResourceAsStream
are definitely the way to go for loading the resource data. However, I don't believe there's any way of "listing" the contents of an element of the classpath.
ClassLoader.getResourceAsStream
并且Class.getResourceAsStream
绝对是加载资源数据的方法。但是,我认为没有任何方法可以“列出”类路径元素的内容。
In some cases this may be simply impossible - for instance, a ClassLoader
couldgenerate data on the fly, based on what resource name it's asked for. If you look at the ClassLoader
API (which is basically what the classpath mechanism works through) you'll see there isn't anything to do what you want.
在某些情况下,这可能根本不可能——例如,aClassLoader
可以根据它要求的资源名称动态生成数据。如果您查看ClassLoader
API(这基本上是类路径机制的工作原理),您会发现没有任何东西可以做您想做的事情。
If you know you've actually got a jar file, you could load that with ZipInputStream
to find out what's available. It will mean you'll have different code for directories and jar files though.
如果你知道你实际上有一个 jar 文件,你可以加载它ZipInputStream
以找出可用的内容。不过,这意味着您将拥有不同的目录和 jar 文件代码。
One alternative, if the files are created separately first, is to include a sort of manifest file containing the list of available resources. Bundle that in the jar file or include it in the file system as a file, and load it before offering the user a choice of resources.
一种替代方法是,如果首先单独创建文件,则包括一种包含可用资源列表的清单文件。将其捆绑在 jar 文件中或将其作为文件包含在文件系统中,并在为用户提供资源选择之前加载它。
回答by topchef
Try this:
尝试这个:
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
There are more methods available, e.g. see here: http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
还有更多可用的方法,例如,请参见此处:http: //www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
回答by Chris Conway
I had the same problem and was able to use the following:
我遇到了同样的问题,并且能够使用以下内容:
// Load the directory as a resource
URL dir_url = ClassLoader.getSystemResource(dir_path);
// Turn the resource into a File object
File dir = new File(dir_url.toURI());
// List the directory
String files = dir.list()
回答by Fish
This is one option: http://www.uofr.net/~greg/java/get-resource-listing.html
这是一种选择:http: //www.uofr.net/~greg/java/get-resource-listing.html
回答by Maurice Rogers
Here is a bit of code from one of my applications... Let me know if it suits your needs. You can use this if you know the file you want to use.
这是我的一个应用程序中的一些代码......让我知道它是否适合您的需求。如果您知道要使用的文件,则可以使用它。
URL defaultImage = ClassA.class.getResource("/packageA/subPackage/image-name.png");
File imageFile = new File(defaultImage.toURI());
Hope that helps.
希望有帮助。
回答by Lukas Masuch
A reliable way to construct a File instance on a resource retrieved from a jar is it to copy the resource as a stream into a temporary File (the temp file will be deleted when the JVM exits):
在从 jar 检索的资源上构造 File 实例的一种可靠方法是将资源作为流复制到临时文件中(当 JVM 退出时临时文件将被删除):
public static File getResourceAsFile(String resourcePath) {
try {
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
if (in == null) {
return null;
}
File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
//copy stream
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
return tempFile;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}