Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java relative path in NetBeans

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?

like image 936
Keshan Avatar asked Feb 23 '23 07:02

Keshan


1 Answers

You will want to use getResource or getResourceAsStream.

Example on java2s.com:

http://www.java2s.com/Code/Java/Development-Class/Loadresourcefilerelativetotheclasslocation.htm

like image 195
Ray Toal Avatar answered Mar 07 '23 10:03

Ray Toal