Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Why is the return type of FileInputStream.read() int, when it returns byte

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Why is the return type of FileInputStream.read() int, when it returns byte

    From the Javadoc,

    Returns:

    the next byte of data, or -1 if the end of the file is reached.
    It almost always return byte, except when the end of the file has been reached and there is no next byte. How can a method even return different data types under different circumstances?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why is the return type of FileInputStream.read() int, when it returns byte

    It's because it can't return different data types.
    It returns an int so that it can return -1 as EOF, which is a left over from the likes of the C libraries.

    In any case, you are unlikely to ever need to use the raw read() methods. I know I've never used them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Why is the return type of FileInputStream.read() int, when it returns byte

    so that it can return -1 as EOF
    What's EOF?

    In any case, you are unlikely to ever need to use the raw read() methods. I know I've never used them.
    I am reading this chapter. I think I have to understand Oracle's tutorials in order to understand Java.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why is the return type of FileInputStream.read() int, when it returns byte

    EOF = End Of File

    And if you do use byte streams, you would be using a buffer and not reading individual bytes.

    For example, to transfer a file of any type from A to B you'd use a byte stream and a byte buffer.
    Java Code:
    int byteCount;
    byte[] buffer = new byte[some suitable buffer size];
    while ((byteCount = myInputStream.read(buffer)) > -1) {
        myOutputStream.write(buffer, 0, byteCount);
    }
    That's the standard structure, though there are probably simpler methods these days that cover it.
    This is the read() method used, and this is the write() method.
    SurfMan likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Why is the return type of FileInputStream.read() int, when it returns byte

    @Tolls OK. Thank you very much.

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 02:15 PM
  2. Replies: 0
    Last Post: 06-10-2013, 10:48 AM
  3. Replies: 2
    Last Post: 06-04-2011, 11:52 PM
  4. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-18-2010, 11:42 PM
  5. Replies: 13
    Last Post: 09-16-2009, 01:05 PM

Posting Permissions

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