This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with file streams

 
John King
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to be able to load the files in my program. Then I can loop through the lines one time, but if I try a second loop to load the strings into an array that one won't work.

Here's my code. I thank you for any help in advance.

Its the 4th while loop that isn't working. The first three are fine.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"DeadJustice," please check your private messages by clicking on My Private Messages. Thanks!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only read from a stream one time, the stream does not reset itself.

You have only three underlying streams in your code. Both times you call url.openStream() a new connection to the URL is made and a new stream is generated, which is used as a source stream for your InputStreamReader/BufferedReader wrappers. Then you make a single, new FileInputStream and use that single stream as the source for two InputStreamReader/BufferedReader wrappers. Because they both delegate to the same FileInputStream you won't be able to read from the second wrapper - the stream has already been depleted.

You may be able to reset() the stream, if BufferedReader, InputStreamReader, and FileInputStream support reset()ing (more appropriately if they support marking, which you can test by calling markSupported()) and be able to read from the FileInputStream again.

Or you are better off cutting your number of reads in half - just read from the URL and the File once, filling a List<String> with the results and converting the List<String> to an array after you are finished reading. It will be much more efficient that way.


[ October 08, 2008: Message edited by: Steve Luke ]
 
John King
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I didn't think to use vectors or lists until just now. Guess I was thinking too hard.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic