tags:

views:

42

answers:

3

I am trying to load a text file from the src package. I don't want to load it as an input stream. I would like to load it as an FileReader.

Looking at how netbeans loads icons, I tried using the code:

getClass().getResourcesAsStream("/getresources/test.txt");

However, I can't find any way to convert an inputstream into a filereader. Is there anyway do do this so that I can use a FileReader. I know I could break the inputstream up into lines myself but that seems like to much work.

Thanks!

nt

+2  A: 

Note that a classpath resource might not be accessible from the file system. Also, I'd assume any decent API would accept a Reader rather than a FileReader. You can do:

Reader reader = new InputStreamReader(inputStream);
Bozho
+1  A: 

Since you have a streamed resource (which could come from a URL) and not a file, i would suggest using an InputStreamReader.

Stroboskop
+3  A: 

You would probably want to use an BufferedReader instead. It has the same benefits of a Filereader, can read line by line etc, and accepts a InputStreamReader which accepts an InputStream. These IO classes are actually an implementation of the well known Decorator Pattern. If you read up on that pattern you will probably understand all these IO classes more.

A FileReader expects a File.

nkr1pt
thx. that worked beautifully!
ntmp