如何在test.property
中访问名为user.home
的属性文件?
这不起作用:
public class Properties {
private static java.util.Properties p = new java.util.Properties();
static {
try {
String home = System.getProperty("user.home");
home += "\\test.properties";
System.out.println("User home: " + home);
InputStream is = Properties.class.getResourceAsStream(home);
p.load(is);
} catch (FileNotFoundException e) {
System.out.println("property file could not be found!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
File propsFile = new File(home, "test.properties");
InputStream is = new FileInputStream(propsFile);
您可能会发现user.home
不在应用程序的运行时类路径中。在这种情况下,请改用File
。