相关疑难解决方法(0)

从jar文件复制目录

我最近开发了一个应用程序并创建了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)

不幸的是,这不起作用.

没有运气我尝试了以下内容:

在写这篇文章的时候,我正在思考而不是在资源路径中有一个模板目录,而是有一个zip文件.这样做我可以将文件作为inputStream并将其解压缩到我需要的位置.但我不确定这是不是正确的方法.

java jar getresource

32
推荐指数
5
解决办法
3万
查看次数

NIO2:如何将 URI 映射到路径?

我正在尝试找到一种简单的方法来将 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)

java filesystems zip jar nio2

4
推荐指数
1
解决办法
3077
查看次数

标签 统计

jar ×2

java ×2

filesystems ×1

getresource ×1

nio2 ×1

zip ×1