英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论