[英]InputStream is null
在我正在研究的程序中
String cwd;
String file_separator;
public ConfigLoader()
{
cwd = get_cwd();
file_separator = get_file_separator();
try
{
Properties c = new Properties();
InputStream in = this.getClass().getClassLoader().getResourceAsStream(cwd +
file_separator + "data" + file_separator + "configuration.properties");
c.load(in);
}
except (Exception e) { e.printStackTrace(); }
}
public String get_file_separator()
{
File f = new File("");
return f.separator;
}
public String get_cwd()
{
File cwd = new File("");
return cwd.getAbsolutePath();
}
但是,出於某種原因, c.load(in);
導致NullPointerException
。 異常來自in == NULL
為真。 我不明白為什么因為
System.out.println(cwd + file_separator + "data" + file_separator +
"configuration.properties");
印刷
/users/labnet/st10/jjb127/workspace/Brewer-Client/data/configuration.properties
這是我要使用的文件的位置。
想法?
getResourceAsStream
用於搜索類路徑中的文件,而不是用於訪問本地文件系統。 在這種情況下,您將不得不使用FileInputStream
。
InputStream in = new FileInputStream(cwd +
file_separator + "data" + file_separator + "configuration.properties");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.