我有一個問題,我無法解決。 我需要讀取屬性文件,但不能設置正確的路徑。 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;
}
}
而且ApplicationController.class它採用PropertyReader:由C
@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";
如果我設置路徑: // ..它工作正常。
謝謝各位大大,有一個愉快的一天!
把屬性的資源文件夾中的文件 –
你是如何創建'文件對象 – Sanjeev
文件文件=新文件(「這裏是文件路徑」); –