• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting BufferedReader to GetSystemResourceAsStream  RSS feed

 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry, this question might be a bit newbie, I'm still getting the hang of this java milarky.

I have written a java program and want to distribute it in a .jar file. But to do so I apparently need to convert my Buffered Readers to GetSystemResourceAsStream instead so I can read from the text files contained within the jar. What I need to know is how do I actually read lines of text from a System Resource Stream object? With BufferedReader I would just use BufferedReader.readline() but I'm not sure what the equivalent method is in GSRAS. I have tried Googling the answer to this but unfortunately haven't come up with anything helpful
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Edit: Just note that you need to take care of the path of your resource.txt file. This code will work on the assumption that the resource.txt and class containing above code are in the same package. If that's not the case, you would need to provide different path to your resource file.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Java streams and readers you usually get the effect you want by wrapping one round another. The outer ones give you the interface you want, and the inner ones give you the capability to read a particular source. In fact, you're probably doing this already. A BufferedReader isn't created by itself, its constructor required you to supply a Reader object. If you're reading from files, I'd guess you're probably using a FileReader for this.

So, getSystemResourceAsStream() and getResourceAsStream return an InputStream. You can get a Reader for this by wrapping it in an InputStreamReader. Then you can create a BufferedReader using that, and use it exactly as before.

Edit: or what Kemal said .
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, I was previously wrapping a FileReader in a BufferedReader

So basically I need to wrap an InputStream in an InputStreamReader and then wrap that in a BufferedReader?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. Read the tutorial: Lesson: Basic I/O.
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, thanks (all three of you)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!