簡體   English   中英

為什么 java 在我的資源文件夾中找不到文件?

[英]Why java can't find the file in my resources folder?

我的項目結構:

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

當我將文件放在主項目文件夾下時:

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

我的電話有效:

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

但是,當我將文件放在資源文件夾中並構建它的相對路徑時,它不起作用:

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)

用戶 @michalk 正確評論說,從我的路徑中刪除前導 '/' 會有所幫助。 我最初認為它沒有,因為那里沒有文件。 我后來添加了文件,省略了前導“/”並得到了我的文件!

很想接受你的回答,@michalk。 或者任何更好的答案。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM