【发布时间】:2017-09-18 19:11:30
【问题描述】:
我正在读取属性文件以获取如下文件路径。
String result = "";
InputStream inputStream = null;
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = GetPropertyValues.class.getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
属性文件的路径如下所示。
configSettingsFilePath = C:\\\\ConfigSetting.xml
现在,当我运行我的代码说找不到文件时,我得到了以下异常。
Creating instance of bean 'configSettingHelper'
configSettingsFilePath = C:\ConfigSetting.xml 2017-09-18 14:47:00 调试 ConfigSettingHelper:42 - ConfigSettingHelper :: ConfigSetting File:configSettingsFilePath = C:\ConfigSetting.xml javax.xml.bind.UnmarshalException - 有关联的例外: [java.io.FileNotFoundException: C:\Java\eclipse\eclipse\configSettingsFilePath = C:\ConfigSetting.xml(文件名、目录名或卷标语法不正确)] 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:246) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
如果我直接在代码中使用“C:\ConfigSetting.xml”而不是从属性文件中读取路径,它会读取文件。
您能否建议我应该在属性文件中使用什么来指定路径?
【问题讨论】:
-
您是否尝试过使用正斜杠?例如
C:/ConfigSetting.xml -
@ChristopherSchneider 您不在 Windows 中使用正斜杠。
-
@AleksandrMukhalov 技术上是正确的,但不是在 Java 中打开文件的上下文中。另外,至于问题,如果您尝试在
C:/的根目录中打开某些内容,则您的应用程序可能无法读取它。尝试将其移动到更可能被读/写的目录,例如C:\Users\Username -
尝试将路径保存在属性文件中而不转义反斜杠。像这样:
configSettingsFilePath = C:\\ConfigSetting.xml. -
@AleksandrMukhalov 我从没说过有。当可以使用单个正斜杠时,它会更加冗长。
标签: java properties