【发布时间】:2016-07-28 10:07:15
【问题描述】:
我有一个无法解决的问题。 我需要读取属性文件,但无法设置正确的路径。 java.io.File 的文档说我必须从 src/... 它不起作用,我从当前文件做了一个路径并且有同样的问题。
例外是:FileNotFound
PropertyReader 类:
public final class PropertyReader {
private Properties prop = new Properties();
private InputStream input = null;
public Properties getProperties(File file) {
try {
input = new FileInputStream(file);
// load a properties file
prop.load(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return prop;
}
}
以及使用 PropertyReader 的 ApplicationController.class:
@RequestMapping(value = "/result", method = RequestMethod.GET)
public String resultPage(ModelMap model) {
//Getting property with key "path"
model.addAttribute("path", new PropertyReader().getProperties(file).getProperty("path"));
return "result";
如果我从 C://.. 设置路径,它工作正常。
非常感谢,祝您有美好的一天!
【问题讨论】:
-
将属性文件放到资源文件夹中
-
你是如何创建
File对象的 -
文件 file = new File("这里是文件路径");
-
属性在资源文件夹中,你可以在项目结构中找到
标签: java web-applications path properties-file