有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么java在我的资源文件夹中找不到该文件?

我的项目结构:

/myProject/
    src/
        main/...
        test/
            resources/
                myFile.txt

当我将文件放在主项目文件夹下时:

/myProject/
    myFile.txt
    src/
        main/...
        test/
            resources/

我的电话有效:

String myFile = "myFile.txt";
isFileThere(myFile);

但是,当我将文件放入resources文件夹并构建其相对路径时,它不起作用:

String myFile = "myFile.txt";
String fullPath = String.format("/src/test/resources/%s", myFile);
isFileThere(fullPath);

***
java.io.FileNotFoundException: \src\test\resources\myFile.txt (The system cannot 
find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    User@michalk正确地评论说,从我的路径中删除前导“/”会有所帮助。 我最初认为没有,因为那里没有文件。 我后来添加了这个文件,省略了前导“/”,得到了我的文件

    我很乐意接受你的回答,@michalk! 或者更好的答案

    谢谢