Reading a resource with BufferedReader

Skogen picture Skogen · Mar 3, 2013 · Viewed 30k times · Source

I am trying to read a resource that will be included into a .JAR, but I get a nullPointer for the following:

bReader = new BufferedReader(new InputStreamReader(
                  this.getClass().getResourceAsStream("resources/" + fileName)));

Using a File however, works fine..

bReader = new BufferedReader(new FileReader(new File("resources/" + fileName)));

Answer

sgp15 picture sgp15 · Mar 3, 2013

Assuming your IDE/Maven/ANT/Gradle/build process will include contents of "resources" in jar at root, try finding it at "/".

bReader = new BufferedReader(new InputStreamReader(
                           this.getClass().getResourceAsStream("/" + fileName)));

UPDATE:

Make sure the "resources" folder is configured as a resource folder.