I am developing a NetBeans module where I have a Java package called testand another package called test.templates. I want to read a text file which is in the test.templates package from a Java file in the test package. I tried in several ways, but it gives a FileNotFoundException exception:
BufferedReader br = new BufferedReader(new FileReader("templates/test.txt"));
BufferedReader br = new BufferedReader(new FileReader("/test/templates/test.txt"));
BufferedReader br = new BufferedReader(new FileReader("src/test/templates/test.txt"));
But none of these worked.. I want to use the relative path, not the absolute path. What should I do?
You will want to use getResource or getResourceAsStream.
Example on java2s.com:
http://www.java2s.com/Code/Java/Development-Class/Loadresourcefilerelativetotheclasslocation.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With