Java - ObjectOutputStream write(int) method



Description

The Java ObjectOutputStream write(int) method writes a byte. This method will block until the byte is actually written.

Declaration

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

public void write(int)

Parameters

  • val − The byte to be written to the stream.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur.

Example - Usage of ObjectOutputStream write(int) method

The following example shows the usage of ObjectOutputStream write(int) 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) {
      int i = 70;
      
      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.write(i);

         // 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.print("" + (char) ois.readByte());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

F

Example - Write a series of ASCII characters as raw bytes

The following example shows the usage of ObjectOutputStream write(int) method. We're writing the ASCII values of characters 'A', 'B', 'C' to a file.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "write_byte1.bin";

      try (FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream oos = new ObjectOutputStream(fos)) {

         oos.write('A'); // ASCII 65
         oos.write('B'); // ASCII 66
         oos.write('C'); // ASCII 67

         System.out.println("Wrote characters: A B C");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read and print
      try (FileInputStream fis = new FileInputStream(filename)) {
         int b;
         fis.skip(6);
         while ((b = fis.read()) != -1) {
            System.out.print((char) b + " ");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote characters: A B C
A B C

Explanation

  • Each call to write(int) writes 1 byte, which corresponds to a character in the ASCII range.

  • When a file is written to ObjectOutputStream, some headers are added. In order to get rid of them in output when the file is read, the 'skip' function is used. In our case, 'skip(6)', 6 bytes are skipped. After skipping first few bytes, the actual data of the file is output. The actual number may vary in your environment. If you see unwanted characters in your output, use 5, 6,7 , etc. and see when it works.

Example - Write byte values manually to simulate a binary format

The following example shows the usage of ObjectOutputStream write(int) method. We're writing raw binary values: 0x01, 0xFF, 0x7F.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "write_byte2.bin";

      try (FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream oos = new ObjectOutputStream(fos)) {

         oos.write(0x01);   // byte value 1
         oos.write(0xFF);   // byte value -1 (255, truncated to 1 byte)
         oos.write(0x7F);   // byte value 127

         System.out.println("Wrote raw bytes: 0x01 0xFF 0x7F");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read and print the hex values
      try (FileInputStream fis = new FileInputStream(filename)) {
         int b;
         while ((b = fis.read()) != -1) {
            System.out.printf("0x%02X ", b);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote raw bytes: 0x01 0xFF 0x7F
0xAC 0xED 0x00 0x05 0x77 0x03 0x01 0xFF 0x7F

Explanation

  • Why the extra bytes at the start? ObjectOutputStream writes a stream header (0xACED0005) at the beginning

java_io_objectoutputstream.htm
Advertisements