Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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);

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top