Thursday, June 11, 2026
HomeLanguagesJavaObjectInputStream available() method in Java with examples

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 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) throws Exception
    {
  
        FileOutputStream out
            = new FileOutputStream("gopal.txt");
        ObjectOutputStream out1
            = new ObjectOutputStream(out);
  
        // write something in the file
        out1.writeUTF("Geeks For Geeks");
  
        // Flushes the Stream
        out1.flush();
  
        // Closes the stream
        out1.close();
  
        // create an ObjectInputStream
        // for the file we created before
        ObjectInputStream example
            = new ObjectInputStream(
                new FileInputStream(
                    "gopal.txt"));
  
        // Print the number of bytes available
        System.out.println(example.available());
        example.close();
    }
}


Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#available()

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS