-1

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

Daniel
  • 49
  • 6
  • where is the code ? no clue what you are trying to do – lesnar Nov 10 '16 at 13:58
  • Sorry about that, added a screenshot. – Daniel Nov 10 '16 at 14:00
  • 1
    If your file is on the classpath, you can get an `InputStream` using `PathFinding.class.getResourceAsStream("com/.../.../.../the_file.txt")`, try replacing the `new File(...)` bit with something like that – BretC Nov 10 '16 at 14:02
  • Check what is actual running directory of your progam, because you use relative path to your file. Simple add a line System.out.println(new File("./").getAbsolutePath()).That relative path you provided must not be in that directory. BTW it is better to start relative path from current directory with "./". I.e. >/com/jenkyn.... etc. – Vadim Nov 10 '16 at 14:24
  • Appreciate the comments, helped me out. – Daniel Nov 10 '16 at 14:34

1 Answers1

0

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/

Community
  • 1
  • 1
lesnar
  • 2,400
  • 7
  • 41
  • 72
  • Thanks, turns out it was in the buildpath however I was referencing the absolute path slightly incorrectly. The link was also useful. – Daniel Nov 10 '16 at 14:32