ObjectInputStream read() method in Java with examples Last Updated : 28 May, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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() Comment More infoAdvertise with us Next Article ObjectInputStream readByte() method in Java with examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-IO package Java-ObjectInputStream Practice Tags : Java Similar Reads ObjectInputStream readByte() method in Java with examples The readByte() method of the ObjectInputStream class in Java is used to read the 8 bit (byte). Syntax: public byte readByte() Parameters: This method does not accept any parameter. Return Value: This method returns the 8 bit byte read Errors and Exceptions: The function throws three exceptions which 2 min read ObjectInputStream readBoolean() method in Java with examples The readBoolean() method of the ObjectInputStream class in Java reads a boolean from the stream. Syntax: public boolean readBoolean() Parameters: This method does not accept any parameter. Return Value: This method returns the boolean that has been read. Errors and Exceptions: The function throws tw 1 min read PushbackInputStream read() method in Java with Examples The read() method of PushbackInputStream class in Java is of two types: The read() method of PushbackInputStream class in Java is used to read the next byte of data from the input stream. This method returns the read byte from the input stream in the form of an integer. Syntax: public int read() thr 4 min read PushbackInputStream unread() method in Java with Examples The unread() method of PushbackInputStream class in Java is of three types: The unread(int b) method of PushbackInputStream class in Java is used to push back a byte by copying it to the front of the pushback buffer. After revoking this method, when the next byte is read it has the value equal to th 6 min read ObjectInputStream close() method in Java with examples The close() method of the ObjectInputStream class in Java closes the input stream. Syntax: public void close() Parameters: This method does not accept any parameter. Return Value: This method does not returns anything. Below program illustrate the above method: Program 1: Java // Java program to ill 1 min read ObjectInputStream available() method in Java with examples The available() method of the ObjectInputStream class in Java returns the number of bytes that can be read without blocking the stream. Syntax: public int available() Parameters: This method does not accept any parameter. Return Value: This method returns the number of available bytes. Below program 1 min read Like