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

[1409 byte] By [GregoryGarretsona] at [2007-10-2 6:13:40]
# 1
you can do this if you wantInputStream is = getClass().getResourceAsStream("fileName");BufferedReader br = new BufferedReader(new InputStreamReader(is));
walken16a at 2007-7-16 13:15:04 > top of Java-index,Desktop,Deploying...
# 2
Thanks, walken16! The InputStreamReader is precisely what I was missing. It seems to work inside and outside of jars, so it fits the bill nicely.Gregory
GregoryGarretsona at 2007-7-16 13:15:04 > top of Java-index,Desktop,Deploying...
# 3

great! glad it's working

here is a statement from the javadoc

[url]http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStreamReader.html[/url]

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

BufferedReader in

= new BufferedReader(new InputStreamReader(System.in));

kind regards

Waken16

walken16a at 2007-7-16 13:15:04 > top of Java-index,Desktop,Deploying...