从 config.file 中读取时抛出 FileNotFoundException(文件或目录不存在)

huangapple go评论70阅读模式
英文:

Reading from config.file throws FileNotFoundException (no such file or directory)

问题

这是我的项目结构:

  • src
    -- java
    --- utils
    ---- ConfigFileReader
    --- resources
    ---- config.properties

这是ConfigFileReader类:

public class ConfigFileReader {

    public static String getProperty(String key) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("/config.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties.getProperty(key);
    }
}

这是我的config.properties文件:

account_storage_technology=cognito

不幸的是,执行该方法会抛出以下异常:

java.io.FileNotFoundException: /config.properties (No such file or directory)

  • 如何修复?
英文:

this is my project structure

- src
-- java
--- utils
---- ConfigFileReader
--- resources
---- config.properties

this is ConfigFileReader class:

public class ConfigFileReader {

    public static String getProperty(String key) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("/config.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties.getProperty(key);
    }
}

and this is my config.properties file:

account_storage_technology=cognito

Unfortunately executing that method throws
> java.io.FileNotFoundException: /config.properties (No such file or directory)

  • How can I fix?

答案1

得分: 1

你需要将resources文件夹与src文件夹保持在同一级目录。像这样:

- src
  - java
    - utils
      - ConfigFileReader
- resources
  - config.properties

并将路径修改如下:

InputStream inputStream = new FileInputStream("resources/config.properties");

更新:不建议将属性文件放在包内并从那里读取。但是,你可以通过显示绝对完整路径使代码读取该文件。

示例:

InputStream inputStream = new FileInputStream("C:\\Project-Path\\Project-Name\\src\\java\\resources\\config.properties");

// Project-Path:这是你的项目父文件夹所在的路径。
// Project-Name:这是你的项目名称。
英文:

You need to keep the resources folder in the same level as src. Like this:

- src
 - java
  - utils
    - ConfigFileReader
- resources
  - config.properties

And modify the path as below :

InputStream inputStream = new FileInputStream("resources/config.properties");

UPDATE : It is not advisable or a good idea to keep properties files inside packages and read it from there. However you can make your code read the file by showing the absolute full path.

Example :

InputStream inputStream = new FileInputStream("C:\\Project-Path\\Project-Name\\src\\java\\resources\\config.properties");

// Project-Path : This is the path where your Project Parent folder resides.
// Project-Name : This is the name of your project.

答案2

得分: 0

当您在开头使用'/'时,它会尝试从您的根目录中进行读取。移除'/',只要在src/main/resources中存在config.properties,FileInputStream就可以选择配置文件。

ConfigFileReader.class.getResourceAsStream("config.properties")
英文:

when you do '/' in the begining it will try to read from your root directory. Remove the / and as long as the config.properties is present in src/main/resources, the FileInputStream should be able to pick the config file.

ConfigFileReader.class.getResourceAsStream("config.properties")

huangapple
  • 本文由 发表于 2020年8月31日 16:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定