0

I want to use a txt file in a jar file.

I am using eclipse.

I have the txt file in a folder called "resources" inside of my project folder.

Here is the code that I am trying to use to get the txt file:

BufferedReader br = new BufferedReader(new FileReader("resources/list of names.txt"));

This works when running the code in eclipse, but it doesn't work after I export it as a .jar file and run it.

Any Suggestions?

Ethan Posner
  • 343
  • 1
  • 2
  • 14

1 Answers1

0

Use Class.getResourceAsStream to get an InputStream.

InputStream is = getClass().getResourceAsStream("/list of names.txt");
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78