我正在尝试为特定文件格式编写转换器。应使用配置文件过滤转换后的文件。到目前为止一切都很好,但是我在读取打包的 jar 中的文件时遇到了问题。
我的文件具有以下结构(Windows):
converter/src/main/resources/files/AStoXES.properties
(还有3个属性文件)
如果我在 Netbeans 中执行此代码,一切正常并且将读取该文件:
this.fileConfig = new PropertiesConfiguration(new File(testing.class.getClassLoader().getResource("files/AStoXES.properties").toURI()));
但是如果我用 Apache Maven
来包装 jar 正在做clean install
并运行整个程序我会得到 java.lang.IllegalArgumentException: URI is not hierarchical
.
在 Google 上花费了数小时的时间后,我发现许多线程都存在相同的问题,并测试了很多东西,但这些建议都没有帮助我解决这个问题。
我需要文件的内容为 java.io.File
,所以使用 Inputstream
与 getResourceAsStream(argument)
不会帮助我,因为我需要使用其中的属性。
有人知道如何解决这个问题吗?
最佳答案
通过点击 AndréStannek 提供的链接 ( Loading a properties file from Java package ) 解决了这个问题。
这样就可以读取了。不知道 PropertiesConfiguration
也接受 InputStreams。
InputStream in = testing.class.getClassLoader().getResourceAsStream("files/AStoCSV.properties");
this.fileConfig = new PropertiesConfiguration();
this.fileConfig.load(in);
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(ConverterControl.class.getName()).log(Level.SEVERE, null, ex);
}
谢谢大家。
关于java - 打开 jar 内的资源 : URI is not hierarchical,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919566/