Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Java - ObjectInputStream readByte() method



Description

The Java ObjectInputStream readByte() method reads a single byte (8-bit value) from the input stream. It is used when a byte value has been written using writeByte(int b).

Declaration

Following is the declaration for java.io.ObjectInputStream.readByte() method −

public byte readByte()

Parameters

NA

Return Value

This method returns the 8 bit byte read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readByte() method

The following example shows the usage of Java ObjectInputStream readByte() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      byte b = 'F';
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeByte(b);
         oout.writeByte('A');
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print a byte
         System.out.println("" + (char) ois.readByte());

         // read and print a byte
         System.out.println("" + (char) ois.readByte());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

F 
A

Example - Writing and Reading a Single Byte

The following example shows the usage of Java ObjectInputStream readByte() method. This example writes a single byte value to a file and reads it back using readByte().

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a single byte value to a file
         FileOutputStream fos = new FileOutputStream("byte_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeByte(100); // Writing byte value 100
         oos.close();

         // Reading byte value using ObjectInputStream
         FileInputStream fis = new FileInputStream("byte_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         byte value = ois.readByte(); // Read a single byte
         System.out.println("Read Byte Value: " + value);

         ois.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read Byte Value: 100

Explanation

  • Writes a single byte value (100) to the file using writeByte(100).

  • Reads the byte value using readByte().

  • Prints the byte value, confirming it was correctly stored and retrieved.

Example - Writing and Reading Multiple Byte Values

The following example shows the usage of Java ObjectInputStream readByte() method. This example writes multiple byte values (65, 66, 67, corresponding to 'A', 'B', 'C') and reads them sequentially.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing multiple byte values
         FileOutputStream fos = new FileOutputStream("multiple_bytes.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeByte(65); // ASCII 'A'
         oos.writeByte(66); // ASCII 'B'
         oos.writeByte(67); // ASCII 'C'
         oos.close();

         // Reading byte values using ObjectInputStream
         FileInputStream fis = new FileInputStream("multiple_bytes.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         System.out.println("First Byte: " + (char) ois.readByte());
         System.out.println("Second Byte: " + (char) ois.readByte());
         System.out.println("Third Byte: " + (char) ois.readByte());

         ois.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

First Byte: A
Second Byte: B
Third Byte: C

Explanation

  • Writes three byte values (65, 66, 67) to a file.

  • Reads them one by one using readByte(), maintaining the original order.

  • Casts bytes to characters (char) to print "A B C".

java_io_objectinputstream.htm
Advertisements