0

I have created a new folder in "scr" with the name "resources". There I have put my file "Test.txt". Now I would like to read in this file with a BufferedReader. This is my current code:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("/resources/Test.txt").getFile());
in = new BufferedReader(new FileReader(file));

But it doesn't work! The file is not found. Why?

LCP
  • 45
  • 1
  • 3
  • 10
  • 3
    Possible duplicate of [How do I load a file from resource folder?](http://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – Praburaj Jan 04 '17 at 12:12
  • Text file needs to be in a 'resources' folder, which needs to be in a folder with a directory path matching the package of the class. If the fully-qualified class is a.b.c.D, then the file needs to be in a/b/c/resources – arcy Jan 04 '17 at 12:15

1 Answers1

1

Make sure you have added the resources folder to your Build Path, and change

classLoader.getResource("/resources/Test.txt").getFile()

to

classLoader.getResource("Test.txt").getFile()
Nathan Meyer
  • 409
  • 1
  • 6
  • 13