我正在尝试为特定文件格式编写一个转换器。转换后的文件应使用配置文件进行过滤。到目前为止,一切工作都很好,但是我在读取打包好的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
包装罐子并执行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);
}
谢谢大家。