Java 如何检查属性文件路径设置是否正确
我对下面的代码有一个简单的定义Java 如何检查属性文件路径设置是否正确,java,Java,我对下面的代码有一个简单的定义 public class PropertyReader { private static Properties propsPath = new Properties(); private static Properties propsConf = new Properties(); private static PropertyReader instance = new PropertyReader(); private Pro
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();
}
}
}
如何检查属性文件路径设置是否正确,以及是否可以从中访问数据?您似乎不太了解系统属性的工作原理,在回答您的问题之前,下面是一个简短的解释: 系统属性用于在启动时将值传递给JVM或在运行时进行设置,可以使用该方法在JVM启动或在运行时进行设置时将关联名称传递给JVM来检索这些值 1-在JVM启动时传递系统属性
java -DmyVar=foo -cp myApp.jar pack.age.MyApp bar bat
在本例中,您可以使用
String myVar = System.getProperty("myVar");
在此调用之后,变量myVar
将具有值foo
bar
和bat
不能通过这种方式检索,它们是应用程序的参数,并按照您在主方法的参数数组中传递它们的顺序显示
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);
}
}
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);
}
}
2-为了选择正确的文件分隔符,您不需要检查应用程序实际运行的操作系统,因为首先,当您启动应用程序时,您已经知道该操作系统可以传递正确的文件路径,第二,java文件系统API可以在幕后为您处理这一问题,只要路径是跨平台兼容的,就可以在任何环境下使用\
和/
,如果省略驱动器号并且应用程序在同一驱动器上启动,则大多数相对路径和windows绝对路径都是如此。因此,您不能在unix系统上使用类似C:\\conf\businessportal.properties
的内容,因为它显然包含一个仅适用于windows主机的驱动器号
3-最后,我看到您的属性文件位于源代码的参考资料下。您将无法直接从文件系统加载它,因为当您的应用程序打包时,路径src/main/resources
将不存在,并且那里的所有内容都将打包到应用程序的存档中,并显示在应用程序的类路径上。然后,您需要使用加载它,并将资源名称传递给它,如下所示:
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);
}
}
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);
}
}
我建议你检查一下那个文件是否存在。但这并不能告诉您路径是否正确,文件是否丢失或无法访问。除此之外,传递路径作为属性键很可能是错误的;如果(f.exists()&&!f.isDirectory()){//您的进一步代码将在这里}我相信它没有链接到spring boot。请阅读
System的用法。getProperty
看起来很奇怪。1) 参数必须是全局系统属性中必须存在的键(2)。我很确定这些要求都不适用于你的情况。谢谢你的精彩解释,现在我有了一个清晰的想法。非常感谢。@Shozab很高兴我能帮上忙,你可能会想,也许也是;-)