I have a structure like:
src -|com -|company -|Core --> JAVA class -|rules --> text files
Inside Core is a Java Class that uses:
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
But I cannot figure out what relative path to use. Now I use:
"..\\..\\..\\..\\rules\\rule1.txt"
I also tried:
"../../../../rules/rule1.txt"
Where did I go wrong?
It doesn't work that way. Relative paths are relative to the working directory of the current application, not the directory of the class. If you want to find out what your current working directory is, use System.getProperty("user.dir")
.
A better way is to add the rules directory to the classpath and then use Class#getResourceAsStream()
.
An alternative would be to set the path to the rules directory somehow else: You could provide a command line option if that is feasible or set some system property (e.g. java -Drules.path=/path/to/rules ...
and later read via System.getProperty("rules.path")
).
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