我有一個簡單的java程序來測試在java中嘗試使用資源,我得到了File Not Found錯誤,程序和文件在同一個包中,有人可以告訴我什麼目錄沒有File資源開始嘗試使用資源Java 7
public class LoadConfigFile {
public static String getProperty(String propertyName) {
String propertyValue = null;
try (InputStream in = new FileInputStream("Properties.properties")) {
Properties prop = new Properties();
prop.load(in);
propertyValue = prop.getProperty(propertyName);
} catch (IOException e) {
System.out.println("Error Reading Property File" + e.getMessage().toString());
}
return propertyValue;
}
}
Properties.properties搜索
properties.one=1
properties.two=2
properties.three=3
properties.four=4
properties.five=5
Main.java
public class Main {
public static void main(String[] args) {
String s = LoadConfigFile.getProperty("property.one");
System.out.println(s);
}
}
有可能你不在你認爲你的目錄工作。這行應該幫助你弄明白:System.out.println(「Active file location is:」+ new File()。getAbsolutePath()); 。只需補充一點,它會被調用,你應該對發生的事情有更好的瞭解。 – Jeutnarg