如何访问user.home目录中的属性文件?

时间:2012-03-19 12:55:16

标签: java properties environment-variables

如何在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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

File propsFile = new File(home, "test.properties");
InputStream is = new FileInputStream(propsFile);

您可能会发现user.home不在应用程序的运行时类路径中。在这种情况下,请改用File