Recommended way to open a text file included in a jar?
Forgive me if this seems like an ignorant question, but I keep reading two different things about loading resources in the javadocs and forums, and can't seem to connect them...
Say you want to open a text file and read the contents into a String or a List. Apparently, the preferred way to do this is something like:
fileName = "myFile.txt";
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
String line;
while ((line = reader.readLine()) != null) {...}
Now say you want to distribute your app in a jar file, with the text file included in the jar file. The above method won't work, because a FileReader operates on a File, and as I understand it, once myFile.txt is packed into the jar file, it's no longer a "file".
So what do you do? I keep reading that the best way to open the jarred text file is to use something like this:
InputStream is = getClass().getResourceAsStream(fileName);
Here's where I get confused: InputStream, and all of its subclasses, are now supposedly dispreferred for reading character data (in preference to Reader, as above). But no subclass of Reader can wrap around an InputStream, which would facilitate the reading of character data greatly, with methods like readLine().
So my question is: What's the best way to read a text file from within a .jar?
Thanks,
Gregory

