我有一个简单的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