aspose file tools*
The moose likes Beginning Java and the fly likes Help with file streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with file streams" Watch "Help with file streams" New topic
Author

Help with file streams

John King
Greenhorn

Joined: Oct 08, 2008
Posts: 3
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

Joined: Aug 31, 2004
Posts: 11343

"DeadJustice," please check your private messages by clicking on My Private Messages. Thanks!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

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 ]

Steve
John King
Greenhorn

Joined: Oct 08, 2008
Posts: 3
Thanks for your help. I didn't think to use vectors or lists until just now. Guess I was thinking too hard.
 
jQuery in Action, 2nd edition
 
subject: Help with file streams