Skip to content
Related Articles

Related Articles

ObjectInputStream read() method in Java with examples

View Discussion
Improve Article
Save Article
  • Last Updated : 28 May, 2019

The read() method of the ObjectInputStream class in Java reads a byte of data. This method wont run if there is no data.


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
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
            // create an ObjectInputStream
            // for the file
            ObjectInputStream example
                = new ObjectInputStream(
                    new FileInputStream(
            // Read from the stream
            for (int i = 0; i < example.available();) {
                System.out.print("" + (char);
        catch (Exception ex) {



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!