Java src/main/resources 中的 FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23547488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileNotFoundException in src/main/resources
提问by Mulgard
i placed a file in my maven project under src/main/resources the files name is simply temp.txt.
我在 src/main/resources 下的 maven 项目中放置了一个文件,文件名只是 temp.txt。
When i try to open the file:
当我尝试打开文件时:
BufferedReader br = new BufferedReader(new FileReader(new File("./temp.txt")));
I get an error:
我收到一个错误:
Exception in thread "main" java.io.FileNotFoundException: \temp.txt
all files under src/main/resources are placed in the root folder of the classpath under maven. So why cant the program find the file here?
src/main/resources下的所有文件都放在maven下classpath的根文件夹下。那么为什么程序在这里找不到文件呢?
采纳答案by Paul Samsotha
If you're going to package the file in the class path, then read it as such.. from the class path.
如果您要在类路径中打包文件,那么从类路径中读取它。
Maven Structure
Maven 结构
src
main
resources
file.txt
After it builds, the file gets placed in the root of the class path. So use
构建后,文件被放置在类路径的根目录中。所以用
InputStream is = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The /
in front of file.txt
will bring you to the root, from whatever package the class is in.
在/
前面file.txt
将带给你的根,无论从任何包装类是英寸
UPDATE
更新
Test example
测试示例
package com.underdogdevs.stackoverflow;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestResourceFile {
public static void main(String[] args) throws IOException {
InputStream is = TestResourceFile.class.getResourceAsStream("/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
回答by gregor
Maven puts the files under /src/main/resouces/
into the default package of your classpath. Hence you can load through the classloader:
Maven 将文件/src/main/resouces/
放入类路径的默认包中。因此,您可以通过类加载器加载:
InputStream in = getClass().getResourcesAsStream("temp.txt")
For more information see Class#getResoucesAsStream.
有关更多信息,请参阅Class#getResoucesAsStream。
回答by Akshay Indalkar
just give this as your path:
只需将此作为您的路径:
BufferedReader br = new BufferedReader(new FileReader(new
File("src/main/resources/temp.txt")));
回答by Adrian Chu
Had this problem when setting up integration tests in Jenkins.
The issue was caused by having the job inside a folder with spaces in the name. So instead of having a workspace folder named foo bar
, Jenkins created foo%20bar
and the tests all failed with FileNotFoundException
.
The solution is just to rename your folder so it doesn't have any spaces.
在 Jenkins 中设置集成测试时遇到了这个问题。
该问题是由于将作业放在名称中带有空格的文件夹中引起的。因此foo bar
,Jenkins 没有创建一个名为 的工作区文件夹,而是创建foo%20bar
了所有测试都失败了FileNotFoundException
.
解决方案只是重命名您的文件夹,使其没有任何空格。