LCP
LCP

Reputation: 45

How can I read my file which is in a resources folder in Java?

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?

Upvotes: 0

Views: 5210

Answers (1)

Nathan Meyer
Nathan Meyer

Reputation: 435

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()

Upvotes: 1

Related Questions