Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectOutputStream writeByte(int val) method



Description

The Java ObjectOutputStream writeByte(int val) method writes an 8 bit byte.

  • Writes the lowest 8 bits of the integer v as a single byte.

  • The value is truncated to a byte (0 to 255), even if the input is a full int.

  • Inherited from DataOutput, which ObjectOutputStream implements.

  • This is not object serialization − it's raw binary writing.

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeByte(int val) method.

public void writeByte(int val)

Parameters

  • val − The byte value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur while writing to the underlying stream.

Example - Usage of ObjectOutputStream writeByte(int val) method

The following example shows the usage of ObjectOutputStream writeByte(int val) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      byte b = 'H';
      
      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('E');

         // close the stream
         oout.close();

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

         // read and print what we wrote before
         System.out.println("" + (char) ois.readByte());
         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 −

H
E

Example - Write a single byte to a file and read it back

The following example shows the usage of ObjectOutputStream writeByte(int val) method. We're writing 65 (which is ASCII 'A') and reading it back.

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "byte1.bin";

      // Write a single byte (value 65 = 'A')
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeByte(65);
         System.out.println("Byte written: 65 (ASCII 'A')");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read the byte
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         byte value = ois.readByte();
         System.out.println("Byte read: " + value + " (char: '" + (char) value + "')");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Byte written: 65 (ASCII 'A')
Byte read: 65 (char: 'A')

Example - Write multiple byte values in a loop

The following example shows the usage of ObjectOutputStream writeByte(int val) method. We're writing the byte values 1 to 5 and print them back.

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "byte_list.bin";

      // Write several byte values
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         for (int i = 1; i <= 5; i++) {
            oos.writeByte(i);
         }
         System.out.println("Wrote bytes: 1 to 5");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read them back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.println("Reading bytes:");
         for (int i = 0; i < 5; i++) {
            System.out.println(" - " + ois.readByte());
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote bytes: 1 to 5
Reading bytes:
 - 1
 - 2
 - 3
 - 4
 - 5
java_io_objectoutputstream.htm
Advertisements