I have a project with 2 packages:
tkorg.idrs.core.searchengines
tkorg.idrs.core.searchengines
In package (2) I have a text file ListStopWords.txt
, in package (1) I have a class FileLoadder
. Here is code in FileLoader
:
File file = new File("properties\\files\\ListStopWords.txt");
But have this error:
The system cannot find the path specified
Can you give a solution to fix it? Thanks.
If you are trying to call
getClass()
from Static method or static block the you can do the following way.You can call
getClass()
on theProperties
object you are loading into.I wanted to parse 'command.json' inside src/main//js/Simulator.java. For that I copied json file in src folder and gave the absolute path like this :
The following line can be used if we want to specify the relative path of the file.
While the answer provided by BalusC works for this case, it will break when the file path contains spaces because in a URL, these are being converted to %20 which is not a valid file name. If you construct the File object using a URI rather than a String, whitespaces will be handled correctly:
The relative path works in in java using the . operator.
So the question is how do you know the path where the java is currently looking?
do a small experiment
Observe the output , you will come to know the current directory where java is looking . From there , simply use the ./ operator to locate your file.
for example if the output is
and your file is present in the folder MyProject simply use
Hope this helps