How do I load a file from resource folder?

My project has the following structure:

/src/main/java/
/src/main/resources/
/src/test/java/
/src/test/resources/

I have a file in /src/test/resources/test.csv and I want to load the file from a unit test in /src/test/java/MyTest.java

I have this code which didn’t work. It complains “No such file or directory”.

BufferedReader br = new BufferedReader (new FileReader(test.csv))

I also tried this

InputStream is = (InputStream) MyTest.class.getResourcesAsStream(test.csv))

This also doesn’t work. It returns null. I am using Maven to build my project.

20 Answers
20

Leave a Comment