0

My project structure:

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

When I put the file under the main project folder:

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

My call works:

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

However, when I put the file in the resources folder and build the relative path to it, it doesn't work:

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)
DraxDomax
  • 1,008
  • 1
  • 9
  • 28
  • @michalk actually, that helped! Would you like to post this as an answer? I will accept. – DraxDomax Oct 23 '19 at 17:10
  • It was just a guess since you have not shown `isFileThere` method. But I assume you are using a `File` so a path that starts with `/` is absolute path - not relative one. Also have a look at [this](https://stackoverflow.com/questions/3844307/how-to-read-file-from-relative-path-in-java-project-java-io-file-cannot-find-th) question – Michał Krzywański Oct 23 '19 at 17:20
  • @michalk well, if you would like to supply an answer, I would love to accept. If not, do you think I should delete this question or mark as duplicate? – DraxDomax Oct 23 '19 at 17:35

1 Answers1

0

User @michalk correctly commented that removing the leading '/' from my path will help. I initially think it didn't, because there was no file there. I later added the file, with the omitted leading '/' and got my file!

Would love to accept your answer, @michalk! Or any better answer.

Thanks

DraxDomax
  • 1,008
  • 1
  • 9
  • 28