Javatpoint Logo
Javatpoint Logo

Java ObjectInputStream read() Method

The read() method of java.io.ObjectInputStream is used to read a byte of data. Some data must be present to read in inputstream. It returns an integer value indicating the number of bytes read. It returns -1 if the end of the stream is reached without reading a single byte.

Syntax

Parameter

No parameter is passed.

Returns

Returns an integer value.

Throws

IOException

Example 1

Output:

End of the stream is reached
Hello
javaTpoint

Example 2

Output:

Hello
End of the stream is reached 
javaTpoint
available byte to read   0

Java ObjectInputStream read() Method

The read() method of ObjectInputStream class is used to read the data and store into an array of bytes. There must be some data in the stream to use this method. This method accepts 3 parameters. The buf is byte array into which data is stored. The Off is starting offset to start reading, and len is the length of bytes to read.

Syntax

Parameter

buf - the buffer into which the data is read

off - the start offset in the destination array buf

len - the maximum number of bytes read

Returns

It returns an integer indicating the number of bytes read. It returns -1 if there is no data present in the stream.

Throws

NullPointerException , IndexOutOfBoundsException ,IOException .

Example 1

Output:

Hello
javaTpoint
No of bytes read  :  6

Example 2

Output:

Hello
 No of bytes read  : 6
javaTpoint





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA