【问题标题】:Configuration.properties (The system cannot find the file specified)Configuration.properties(系统找不到指定的文件)
【发布时间】:2018-08-26 23:30:39
【问题描述】:

我在类路径中创建了一个带有 Configuration.properties 文件的 spring-boot 应用程序。但是当我打包它并运行应用程序时,我得到了错误 - “Configuration.properties(系统找不到指定的文件)。” 在以下方法中调用它: 公共静态属性 readProperties() 抛出 IOException {

    FileReader reader=new FileReader("Configuration.properties");
    Properties p=new Properties(); 
    p.load(reader);
    return p;

我是spring的新手,spring-boot。有没有办法在spring-boot应用程序的application.properties(内部资源文件夹)中提供值。 或者为什么我创建的属性文件没有进入 jar(打包时)。 有人可以帮忙吗?

问候, 阿尔宾

【问题讨论】:

标签: java maven spring-boot properties properties-file


【解决方案1】:

这是因为该文件是打包在你的jar文件中,而不是文件系统上的常规文件。如果你想从类路径访问一个文件,你应该通过以下方式访问它:

Properties properties = new Properties();
try (InputStream stream =
       this.getClass().getResourceAsStream("/Configuration.properties")) {
    properties.load(stream);
}

但是,使用 Spring Boot,您通常不会直接读取属性文件,除非您出于某种原因确实需要它。还有其他机制可以实现这一点。查看 Spring 文档 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 2023-03-17
    • 2017-12-21
    • 2015-09-28
    • 2015-11-08
    • 2019-01-20
    • 2014-03-24
    相关资源
    最近更新 更多