0
我有一个项目,我将其转换为可运行jar文件。我正在使用ini4j
在我的ini文件中存储一些配置。将.ini文件转换为可运行jar文件时
它工作时,我简单地设置目录getConf = new Ini(new FileReader(path));
,但它不会工作,当我使用getResourceAsStream()
public class IniReader {
// When I set it like that, it works..
private static String path = "../conf.ini";
public static String readIni(String val, String getOb, String fetchOb) {
Ini getConf = null;
try {
// When I set it like that, it works..
getConf = new Ini(new FileReader(path));
// I want to use this version but I am getting null error.
//getConf = new Ini(new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("conf.ini"))));
} catch (InvalidFileFormatException e) {
System.err.print("Error InvalidFileFormat : " + e.getMessage() + "\n");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.err.print("Error FileNotFoundException : " + e.getMessage() + "\n");
e.printStackTrace();
} catch (IOException e) {
System.err.print("Error IOException : " + e.getMessage() + "\n");
e.printStackTrace();
}
return val = getConf.get(getOb).fetch(fetchOb);
}
当我尝试阅读我的ini文件我收到以下错误;
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.ini4j.Ini.load(Ini.java:104)
at org.ini4j.Ini.<init>(Ini.java:56)
at com.test.ttf.IniReader.readIni(IniReader.java:28)
at com.test.ttf.SlashSCR.InitProg(SlashSCR.java:116)
at com.test.ttf.InitProg.main(InitProg.java:18)
这是我想读.ini文件
编辑
我也试过以下;
Ini getConf= new Ini();
getConf.load(Thread.currentThread().getContextClassLoader().getClass().getResourceAsStream("../conf.ini"));
谢谢!我打算制作这个文件的.exe和.app版本,我应该将我的.ini文件放在资源目录中,并在exe和app文件旁边生成快捷方式,或者我应该直接让用户通过原始文件访问ini文件目录? – 2015-02-09 13:47:31
这并不重要,只要它们与运行位置相比处于相同的相对位置即可。当您使用快捷方式时,设置运行目录是快捷方式的一个属性(所以这可能会使它更复杂一点)。 – Thirler 2015-02-09 14:11:25