我最近开发了一个应用程序并创建了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);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.
没有运气我尝试了以下内容:
使用Streams解决其他类的类似问题,但它不适用于dirs.代码类似于 http://www.exampledepot.com/egs/java.io/CopyFile.html
使用创建文件模板 new File(getClass().getResource("template").toUri())
在写这篇文章的时候,我正在思考而不是在资源路径中有一个模板目录,而是有一个zip文件.这样做我可以将文件作为inputStream并将其解压缩到我需要的位置.但我不确定这是不是正确的方法.
我正在尝试找到一种简单的方法来将 a 映射URI到 a Path,而无需编写特定于任何特定文件系统的代码。以下似乎可行,但需要一种有问题的技术:
public void process(URI uri) throws IOException {
try {
// First try getting a path via existing file systems. (default fs)
Path path = Paths.get(uri);
doSomething(uri, path);
}
catch (FileSystemNotFoundException e) {
// No existing file system, so try creating one. (jars, zips, etc.)
Map<String, ?> env = Collections.emptyMap();
try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
Path path = fs.provider().getPath(uri); // yuck :(
// assert path.getFileSystem() == fs;
doSomething(uri, path);
}
} …Run Code Online (Sandbox Code Playgroud)