ObjectInputStream read() method in Java with examples
The read() method of the ObjectInputStream class in Java reads a byte of data. This method wont run if there is no data.
Syntax:
public int read()
Parameters: This method does not accept any parameter.
Return Value: This method returns the byte read, or -1 if the end of the stream is reached.
Exceptions: The function throws an IOException if an I/O error has occurred.
Below program illustrate the above method:
Program 1:
// Java program to illustrate // the above method import java.io.*; public class GFG { public static void main(String[] args) { try { // create a new file // with an ObjectOutputStream FileOutputStream out = new FileOutputStream("gopal.txt"); ObjectOutputStream out1 = new ObjectOutputStream(out); // write out1.writeUTF("Geeks for Geeks"); // Flushes the stream out1.flush(); // create an ObjectInputStream // for the file ObjectInputStream example = new ObjectInputStream( new FileInputStream( "gopal.txt")); // Read from the stream for (int i = 0; i < example.available();) { System.out.print("" + (char)example.read()); } } catch (Exception ex) { ex.printStackTrace(); } } } |
Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#read()
Recommended Posts:
- ObjectInputStream available() method in Java with examples
- ObjectInputStream readBoolean() method in Java with examples
- ObjectInputStream close() method in Java with examples
- Reader read() method in Java with Examples
- CharArrayReader read() method in Java with Examples
- PushbackReader read() method in Java with Examples
- StringReader read() method in Java with Examples
- Reader read(CharBuffer) method in Java with Examples
- CharArrayReader read(char[]) method in Java with Examples
- StringReader read(char[], int, int) method in Java with Examples
- CharArrayReader read(CharBuffer) method in Java with Examples
- CharArrayReader read(char[], int, int) method in Java with Examples
- StringReader read(char[]) method in Java with Examples
- Reader read(char[], int, int) method in Java with Examples
- StringReader read(CharBuffer) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



