【发布时间】:2019-05-17 14:25:22
【问题描述】:
我目前正在制作一款具有预先制作的关卡的游戏,并且我目前将它们存储在资源中。我想要一个解决方案来解决如何在生产和开发环境中从 jar 中提取文件夹。
我尝试使用下面给定的方法复制文件夹并将 src 传递为File defaultWorld = new File(GameData.class.getClassLoader().getResource("worlds/").getFile());
和目的地为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。
【问题讨论】:
标签: java file-io java-8 game-development