Daniel Daniel - 25 days ago 20
Java Question

File Not found, is my package setup correctly

I should first mention that I'm still an undergraduate, so excuse "beginner" mistakes, I'm also using the intellij IDE.

It's entirely possible that I'm missing something rather obvious but why is this .txt file not found when it's in a source root. My package hierarchy looks to be fine.

Would appreciate if anyone could point me in the right direction, thanks in advance.

As you can see fileNotFoundException

Answer

Seems like your project is not on build path . On Intellij you can configure it as mentioned in this post:

How to add a project to build path in IntelliJ Idea

And then you can access it using absolute path :

getClass().getResourceAsStream("/com/path/to/file.txt");

Or relative CLASSPATH path (when the class you are writing is in the same Java package as the resource file itself):

getClass().getResourceAsStream("file.txt");

I would also suggest you to read here: https://www.mkyong.com/java/java-read-a-file-from-resources-folder/

Comments