Sunday, February 15, 2026
HomeLanguagesJavaObjectInputStream readByte() method in Java with examples

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 is described below:

  • EOFException: The exception is thrown if the end of file is reached.
  • IOException: The exception is thrown 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)
        throws IOException
    {
        byte[] array
            = { 1, 34, 23,
                42, 69, 22 };
  
        try {
  
            // create new byte
            // array input stream
            InputStream input
                = new ByteArrayInputStream(array);
  
            // create data input stream
            DataInputStream output
                = new DataInputStream(input);
  
            // readBoolean till the
            // data available to read
            while (output.available() > 0) {
  
                // read one single byte
                byte bt = output.readByte();
  
                // print the byte
                System.out.print(bt + " ");
            }
        }
  
        catch (Exception ex) {
        }
    }
}


Output:

Program 2:

Java




// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws IOException
    {
        byte[] array
            = { 'G', 'e', 'e', 'k',
                's', 'f', 'o', 'r',
                'g', 'e', 'e', 'k',
                's' };
  
        try {
  
            // create new byte
            // array input stream
            InputStream input
                = new ByteArrayInputStream(array);
  
            // create data input stream
            DataInputStream output
                = new DataInputStream(input);
  
            // readBoolean till the
            // data available to read
            while (output.available() > 0) {
  
                // read one single byte
                byte bt = output.readByte();
  
                // print the byte
                System.out.print(bt + " ");
            }
            System.out.println();
            System.out.println();
        }
  
        catch (Exception ex) {
        }
    }
}


Output:

Reference:

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32505 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6880 POSTS0 COMMENTS
Nicole Veronica
12003 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12097 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6966 POSTS0 COMMENTS
Umr Jansen
6954 POSTS0 COMMENTS