简体   繁体   English

src / main / resources中的FileNotFoundException

[英]FileNotFoundException in src/main/resources

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. src / main / resources下的所有文件都放在maven下的classpath的根文件夹中。 So why cant the program find the file here? 那么为什么程序无法在这里找到文件呢?

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 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);
        }
    }
}

在此输入图像描述

在此输入图像描述

just give this as your path: 把它作为你的道路:

BufferedReader br = new BufferedReader(new FileReader(new 
File("src/main/resources/temp.txt")));

Had this problem when setting up integration tests in Jenkins. 在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 . 因此,Jenkins没有一个名为foo bar的工作区文件夹,而是使用FileNotFoundException创建了foo%20bar并且所有测试都失败了。

The solution is just to rename your folder so it doesn't have any spaces. 解决方案只是重命名您的文件夹,因此它没有任何空格。

Maven puts the files under /src/main/resouces/ into the default package of your classpath. Maven将/src/main/resouces/下的文件放入类路径的默认包中。 Hence you can load through the classloader: 因此,您可以通过类加载器加载:

InputStream in = getClass().getResourcesAsStream("temp.txt")

For more information see Class#getResoucesAsStream . 有关更多信息,请参阅Class#getResoucesAsStream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileNotFoundException:src \\ main(访问被拒绝) - FileNotFoundException: src\main (Access is denied) 在我的SpringBoot项目中,当我将文件目录更改为src / main / resources时,出现FileNotFoundException异常 - In my SpringBoot project, an FileNotFoundException occred when i change the file directory to src/main/resources java.io.FileNotFoundException:即使文件存在于 src/main/resources 中,类路径资源 - java.io.FileNotFoundException: class path resource even though file exists in src/main/resources java.io.FileNotFoundException:/home/ec2-user/src/main/resources/keystore.jks(无此类文件或目录) - java.io.FileNotFoundException: /home/ec2-user/src/main/resources/keystore.jks (No such file or directory) Gradle和src / main / resources - Gradle and src/main/resources 错误原因:SpringBoot 2.0 应用程序中 src/main/resources/config/env/dev2 下的文件的 java.io.FileNotFoundException - Error Caused by: java.io.FileNotFoundException for file under src/main/resources/config/env/dev2 in SpringBoot 2.0 application java.io.FileNotFoundException: src\\main\\resources\\Report\\testQuery.xlsx (系统找不到指定的路径) 在tomcat中部署文件后 - java.io.FileNotFoundException: src\main\resources\Report\testQuery.xlsx (The system cannot find the path specified) after deploying file in tomcat src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath src / main / resources中的Maven Jar主类 - Maven Jar main class in src/main/resources 让 Eclipse 使用 src/test/resources 而不是 src/main/resources - Make Eclipse use src/test/resources instead of src/main/resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM