Tag: getresource

从jar访问jar外的资源

我正在尝试从jar文件访问资源。 资源位于jar所在的同一目录中。 my-dir: tester.jar test.jpg 我尝试了不同的东西,包括以下内容,但每次输入流为空时: [1] String path = new File(“.”).getAbsolutePath(); InputStream inputStream = this.getClass().getResourceAsStream(path.replace(“\\.”, “\\”) + “test.jpg”); [2] File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + “test.jpg”); 你能给我一些提示吗? 谢谢。

从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); } 不幸的是,这不起作用。 没有运气我尝试了以下内容: 使用Streams来解决其他类中的类似内容,但它不适用于dirs。 代码类似于http://www.exampledepot.com/egs/java.io/CopyFile.html 使用new File(getClass().getResource(“template”).toUri())创建文件模板new File(getClass().getResource(“template”).toUri()) 在写这篇文章的时候,我正在思考而不是在资源路径中有一个模板目录,而是有一个zip文件。 这样做我可以将文件作为inputStream并将其解压缩到我需要的位置。 但我不确定这是不是正确的方法。