Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot locate file location

Tags:

java

I'm trying to scan a text file. Yes, I know... this is simple. I've been trying to locate the path of my text file and nothing works, it gives me a NoSuchFileException

for (String line :  Files.readAllLines(Paths.get("/com/panda/anagramsolver/handling/word_list.txt"))) {
    System.out.println(line);
}

The file I'm trying to scan is located at com.panda.anagramsolver.handling.word_list.txt. I know this question will get a lot of downvotes, I couldn't elaborate my question enough to find it on google. In advance, thanks.

like image 504
Vicente Bermudez Avatar asked Jul 22 '26 17:07

Vicente Bermudez


1 Answers

The path starts with a /. So unless you mean to search inside a directory named com in your root directory, you will fail to locate the file.

You might want to try executing something like System.out.println(new File(".")) to find the location at which the program is executed, and then build up a relative path from that location. But at a more fundamental level, if the program is intended to work with that file always (i.e., if the file is a resource for the program), you might want to turn to Class#getResourceAsStream(). This link is to a tutorial.

like image 184
Jae Heon Lee Avatar answered Jul 24 '26 07:07

Jae Heon Lee