Results 1 to 6 of 6
  1. #1
    jmu2101 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Java IO, how does inputstream/reader read() work, why the while loop

    why is there always a while loop in code snippets online
    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Java IO, how does inputstream/reader read() work, why the while loop

    So what is the while loop doing in every code snippet which uses this read?
    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.

    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.

  4. #4
    jmu2101 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default 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 10:35 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Java IO, how does inputstream/reader read() work, why the while loop

    not sure about how often the int gets returned
    An int value gets returned on EVERY and ALL calls.

    Read the API doc for the read() method for a description of what it does and what it returns.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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

  1. Line Reader changeing to read 1 line
    By Javanooby in forum New To Java
    Replies: 15
    Last Post: 05-10-2011, 05:34 AM
  2. Simple file reader won't work in eclipse
    By BoomPony in forum New To Java
    Replies: 3
    Last Post: 11-27-2010, 06:16 PM
  3. I feel dumb asking this... File reader loop
    By Adomini in forum New To Java
    Replies: 5
    Last Post: 10-30-2010, 02:18 PM
  4. How to read multiple ZipEntry using single InputStream?
    By prasannadavid in forum New To Java
    Replies: 5
    Last Post: 09-08-2010, 02:31 PM
  5. Unable to read data from inputstream
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 04:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •