我最近开发了一个应用程序并创建了 jar 文件。
我的一个课程创建了一个输出目录,并用其资源中的文件填充它。
我的代码是这样的:
// Copy files from dir "template" in this class resource to output.
private void createOutput(File output) throws IOException {
File template = new File(FileHelper.URL2Path(getClass().getResource("template")));
FileHelper.copyDirectory(template, output);
}
不幸的是,这不起作用。
我尝试了以下但没有运气:
使用 Streams 解决其他类上的类似问题,但它不适用于 dirs。代码类似于 http://www.exampledepot.com/egs/java.io/CopyFile.html
使用
new File(getClass().getResource("template").toUri())
在写这篇文章时,我正在考虑在资源路径中有一个模板目录而不是一个 zip 文件。这样做我可以将文件作为 inputStream 获取并在需要的地方解压缩。但我不确定这是否是正确的方法。
原文由 Macarse 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为您使用 zip 文件的方法很有意义。大概你会做一个
getResourceAsStream
来获取 zip 的内部结构,它在逻辑上看起来像一个目录树。骨架方法: