-2

I am trying to read a text file from inside a .jar file and use a Scanner on it. The text file movies.txt is located in the src directory together with the java file in my IDE. When I build the jar file, both the class file and the text file are in the root directory.

InputStream file = this.getClass().getResourceAsStream("/movies.txt");
Scanner scanner = new Scanner(file);

This does not seem to locate the text file. What am I doing wrong here?

Mikkel
  • 482
  • 5
  • 11

1 Answers1

6

Try this:

FileInputStream inputStream = new FileInputStream("/movies.txt");
try {
    String text = IOUtils.toString(inputStream);
} finally {
    inputStream.close();
}
Nehorai Elbaz
  • 2,412
  • 8
  • 17