1

我有以下代码。

public class PropertyReader {

    private static Properties propsPath = new Properties();
    private static Properties propsConf = new Properties();
    private static PropertyReader instance = new PropertyReader();

    private PropertyReader() {

        readConfProperty();
    }

    private void readConfProperty() throws RuntimeException {

        try {
            String path = "";

            if (System.getProperty("os.name").contains("Linux")) {
                //path = System.getProperty("catalina.home") + "conf/businessportal.properties";
                path = System.getProperty("/busDir/src/main/resources/businessportal.properties");
            } else {
               // path = System.getProperty("catalina.home") + "\\conf\\businessportal.properties";
                path = System.getProperty("/busDir/src/main/resources/businessportal.properties");
            }

            Reader reader = new FileReader(path);
            propsConf.load(reader);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

如何检查属性文件路径是否设置正确并可以从中访问数据?

4

1 回答 1

3

您似乎不太了解系统属性的工作原理,在回答您的问题之前,这里是镜头解释:

系统属性用于在启动时将值传递给 JVM 或在运行时设置它们,可以使用System#getProperty方法检索这些值,方法是在 JVM 启动时或在运行时设置时向其传递相关名称。

1-在 JVM 启动时传递系统属性

java -DmyVar=foo -cp myApp.jar pack.age.MyApp bar bat

myVar通过此示例,您可以使用检索

String myVar = System.getProperty("myVar");

在此调用之后,变量myVar将具有值foobar并且bat不能以这种方式检索,这些是应用程序的参数,并按照您在 main 方法的参数数组中传递它们的顺序出现。

2 - 在运行时设置系统属性

System.setProperty("myVar", "foo")

稍后使用与之前相同的调用,您可以再次检索该值。

现在回答你的问题:

1-您在活动代码中的调用不正确,因为这/busDir/src/main/resources/businessportal.properties似乎是您愿意获得的值,而不是您定义的名称,将在您的应用程序中用于查找它。

您应该使用类似这样的方法传递值,如上所示,-D在启动应用程序时设置参数:

-Dbusinessportal.config.path=/busDir/src/main/resources/businessportal.properties

然后用你的方法检索它

String path = System.getProperty("businessportal.config.path");

此时,您将拥有一个包含配置文件路径的字符串,您可以继续检查它的存在,如下所示:

private void readConProperty() throws IOException {
    String strPath = System.getProperty("businessportal.config.path");
    if (null == strPath) {
        throw new RuntimeException("property businessportal.config.path not defined");
    }

    try (InputStream is = Files.newInputStream(Paths.get(strPath))) {
        propsConf.load(is);
    } catch (IOException e) {
        throw new RuntimeException(String
            .format("Error while loading config from %s", strPath), e);
    }
}

2-您不需要检查应用程序实际运行的操作系统来选择正确的文件分隔符,因为首先,当您的应用程序启动时,您已经知道它可以传递正确的文件路径,其次java Filesystem APIs在后台为您处理这个问题,只要路径是跨平台兼容的,就可以在每个环境中工作,如果省略驱动器号并且您的应用程序启动,大多数相对路径和Windows绝对路径都是这种\情况/在同一个驱动器上。因此,您不能C:\\conf\businessportal.properties在 unix 系统上使用类似的东西,因为显然它包含一个驱动器号,该驱动器号仅适用于 Windows 主机。

3-最后,我看到您的属性文件位于源代码中的资源下。您将无法直接从文件系统加载它,因为当您的应用程序被打包时,该路径src/main/resources将不存在,并且所有内容都将被打包到您的应用程序存档中,并且将出现在您的应用程序的类路径中。然后,您需要使用Class#getResourceAsStream加载它,并将资源名称传递给它,如下所示:

private void readConProperty() {
    String resourceName = "/businessportal.properties";
    try (InputStream is = getClass().getResourceAsStream(resourceName)) {
        if (null == is) {
            throw new RuntimeException(String
                .format("Resource %s is not available", resourceName));
        }
        propsConf.load(is);
    } catch (IOException e) {
        throw new RuntimeException(String
            .format("Error while loading config from %s", resourceName), e);
    }
}
于 2018-11-26T11:07:31.333 回答