如何在运行时和dev-env中从jar复制资源不足的文件夹? [英] How to copy folder's out of resources from jar both in runtime and dev-env?
问题描述
我目前正在制作一款具有预先制作的关卡的游戏,并且目前将它们存储在资源中.我想要一个解决方案,以解决在生产和开发环境中如何从jar中提取文件夹的问题.
I am currently making a game which have levels which are pre-made and i currently store them in resources. I want a solution to how i can extract a folder out of a jar in production and development environment.
我尝试使用下面的给定方法复制文件夹,并将src传递为File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile());
目的地为private static File worldsDir = new File("run/worlds");
I have tried copying the folder by using the given method below and pass the src as File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile());
and destination as private static File worldsDir = new File("run/worlds");
public static void copyFolder(File src, File dest) {
try {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String[] files = src.list();
for (String file : files) {
copyFolder(new File(src, file), new File(dest, file));
}
} else {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
我希望上述方法可同时用于开发环境和生产环境,但是在打开文件输出流时会抛出FileNotFoundException
.
I expected the above method to work on both dev and production env but it throws FileNotFoundException
when opening file output stream.
推荐答案
您无法列出jar中的资源.
You cannot list resources in a jar.
您所考虑的任何解决方法都不可靠.
Any workarounds you’re thinking of, are unreliable.
- 从不调用URL的
getFile()
方法.它不返回有效的文件名;它只返回URL的路径和查询部分,并保留所有百分号转义.此外,jar条目不是file:
URL,因此,当资源路径引用jar条目时,它永远不能是有效的文件名. - 列出jar文件中所有内容的唯一方法是遍历所有jar条目,但是甚至不能保证可以访问jar,因为不能保证ClassLoaders是URLClassLoaders,而且通常也不能保证使用
jar:
URL. - 您甚至不能依靠MyApplication.class.getProtectionDomain().getCodeSource(),因为
- Never call the
getFile()
method of URL. It does not return a valid file name; it just returns the path and query portions of the URL, with any percent-escapes intact. Furthermore, jar entries are notfile:
URLs, so a resource path can never be a valid file name when it refers to a jar entry. - The only way to list things in a jar file is by iterating through all jar entries, but you aren’t even guaranteed to have access to your jar, because ClassLoaders are not guaranteed to be URLClassLoaders, and in general are not guaranteed to use
jar:
URLs. - You can’t even rely on MyApplication.class.getProtectionDomain().getCodeSource(), because getCodeSource() can return null.
如果您要从jar中复制多个文件,可以使用以下几种可靠的方法:
If you want to copy multiple files from your jar, here are some reliable ways to do it:
- 对计划复制的资源列表进行硬编码.这是您的应用程序,因此您知道要在jar中放入哪些文件.
- 在jar中保留一个文本文件,其中包含要复制的资源路径列表.
- 将您的资源存储在jar中嵌入的单个zip归档文件中,并使用
- Hard code the list of resources you plan to copy. It’s your application, so you know what files you’re putting in the jar.
- Keep a single text file in your jar which contains a list of resource paths to copy.
- Store your resources in a single zip archive which is embedded in your jar, and extract it yourself with a ZipInputStream which wraps MyApplication.class.getResourceAsStream.
这篇关于如何在运行时和dev-env中从jar复制资源不足的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!