Results 1 to 6 of 6
- 11-08-2011, 09:22 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
Java IO, how does inputstream/reader read() work, why the while loop
How does inputstream, or reader work in java, and why is there always a while loop in code snippets online?
while ((read = reader.read(buf)) >= 0)
{
writer.write(buf, 0, read);
}
about the reader:
"Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached."
So what is the while loop doing in every code snippet which uses this read? If the method automatically calls input stream characters into an array, why do you need to program the code to loop?
Java api 7 states the int returned by reader is: "The number of characters read, or -1 if the end of the stream has been reached".
So how often does this int returned update itself? for every character? Or before it starts and once the reader is finished?
Thanks for your info.
- 11-08-2011, 10:43 PM #2
Re: Java IO, how does inputstream/reader read() work, why the while loop
The call to the read() method does NOT always receive all the data that was sent. So you need to keep using the read method until it tells you there is no more data. With data coming over the internet it can be split up into several pieces. Each read can return the next piece of data. You need to continue reading until read tells you there is no more data.why is there always a while loop in code snippets online
- 11-08-2011, 10:48 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
Re: Java IO, how does inputstream/reader read() work, why the while loop
It is performing a read into the array buf over and over again until all the data has been read (EOF) or there is an error. How many times this will happen depends on the size of the array which might be quite small relative to what is being read. Basically you are reading one array (buffer) full at a time.So what is the while loop doing in every code snippet which uses this read?
Notice that inside the loop you should not write the all of the array - only the number of characters that were actually read.
Have a read of Basic I/O in Oracle's Tutorial for details.
- 11-10-2011, 09:27 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
Re: Java IO, how does inputstream/reader read() work, why the while loop
Thanks for your info. Still not sure about how often the int gets returned, and whether you could put an if statement directly below the int read, like: do{
read = reader.read(buf);
if (read!=-1){writer.write(buf, 0, read);}
} while(read>0);Last edited by jmu2101; 11-10-2011 at 09:35 PM.
- 11-10-2011, 11:18 PM #5
Re: Java IO, how does inputstream/reader read() work, why the while loop
An int value gets returned on EVERY and ALL calls.not sure about how often the int gets returned
Read the API doc for the read() method for a description of what it does and what it returns.
- 11-10-2011, 11:51 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
Re: Java IO, how does inputstream/reader read() work, why the while loop
If you are unsure what the docs and Tutorial are saying, play with some code. What you posted would be an example. If the results are weird, post code, description of observed behaviour and your thoughts: it's sure to get a response.
Similar Threads
-
Line Reader changeing to read 1 line
By Javanooby in forum New To JavaReplies: 15Last Post: 05-10-2011, 04:34 AM -
Simple file reader won't work in eclipse
By BoomPony in forum New To JavaReplies: 3Last Post: 11-27-2010, 05:16 PM -
I feel dumb asking this... File reader loop
By Adomini in forum New To JavaReplies: 5Last Post: 10-30-2010, 01:18 PM -
How to read multiple ZipEntry using single InputStream?
By prasannadavid in forum New To JavaReplies: 5Last Post: 09-08-2010, 01:31 PM -
Unable to read data from inputstream
By renuka_anil in forum Java ServletReplies: 0Last Post: 01-29-2009, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks