首页 文章

spring mvc properties - java.io.FileNotFoundException:config.properties(系统找不到指定的文件)

提问于
浏览
1

我已经创建了一个属性文件,并试图在我的Spring DAO类文件中访问它,但我得到了 "java.io.FileNotFoundException: config.properties (The system cannot find the file specified)" . 我尝试过不同的方案将文件放在不同的文件夹/位置但遇到同样的问题 . 任何人都可以帮我这个 . 以下是代码和结构细节,

在DAO班,

FileReader reader = new FileReader("config.properties");
Properties properties = new Properties();
properties.load(reader);

我试图将"config.properties"文件放在 src/main/resources 下,也放在 WEB-INF/ 下 . 我的DAO课程在 src/main/java/com/test/dao 谢谢你 .

1 回答

  • 0

    您可以将文件保存在最合适的src / main / resources中,并将其检索方式更改为:

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("config.properties").getFile());
    FileReader reader = new FileReader(file);
    

相关问题