EDIT: I want this to work not only in the IDE (when it runs from the root directory) but also when I run it from Terminal (I have cd
'd into the src
directory.
I have a file called main.java.org.name.nlp.lg.Class
, which is in the src
directory. Outside the src
directory (in the root directory), I have a folder called data
which contains the file data/en/words/file.txt
. How can I load file.txt
into Class.java
via a relative path? My current code gives me a NPE:
URL url = new Class().getClass().getResource("../../../../../../../data/en/words/file.txt");
File f = new File(url.getPath());
List<String> list = Files.readAllLines(f.toPath());
System.out.println(list);
Here is the file tree:
src
main
java
org
name
nlp
lg
Class
data
en
words
file.txt
Thanks.
Path pathToYourFile = Paths.get(Paths.get("data/en/words").toAbsolutePath().toString(),
"file.txt");
File yourFile = pathToYourFile.toFile();