Java - ObjectOutputStream write(byte[] buf) method



Description

The Java ObjectOutputStream write(byte[] buf) method writes an array of bytes. This method will block until the bytes are actually written.

Declaration

Following is the declaration for java.io.ObjectOutputStream.write(byte[] buf) method.

public void write(byte[] buf)

Parameters

buf − The data to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur.

Example - Usage of ObjectOutputStream write(byte[] buf) method

The following example shows the usage of ObjectOutputStream write(byte[] buf) 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 = 'a';
      
      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(b);

         // 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 a string
         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 −

a

Example - Write raw byte array data to a file using ObjectOutputStream

The following example shows the usage of ObjectOutputStream write(byte[] buf) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = "Hello, byte stream!".getBytes();

      FileOutputStream fos = new FileOutputStream("raw_bytes1.bin");
      fos.write(data);  // Write raw byte array
      System.out.println("Byte array written to file.");

      FileInputStream fis = new FileInputStream("raw_bytes1.bin");
      byte[] dat = fis.readAllBytes();
      String content = new String(dat);
      System.out.println("Content after skipping header: " + content);       
   }
}

Output

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

Byte array written to file.
Content after skipping header: Hello, byte stream!

Explanation

  • The byte array is written directly to the file.

  • This is not serialized as an object, just raw binary data.

  • You could read it back using a FileInputStream.

Example - Write a byte array followed by a serialized object

The following example shows the usage of ObjectOutputStream write(byte[] buf) method. We're combining low-level byte writing and object serialization in the same stream.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      byte[] header = {0x01, 0x02, 0x03}; // custom header before object data

      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("mixed_data.bin"))) {
         oos.write(header);  // write raw bytes (e.g. versioning info)
         oos.flush();

         // write a serializable object afterward
         oos.writeObject(new Message("Hello from serialized object!"));
         System.out.println("Header and object written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   static class Message implements Serializable {
      private static final long serialVersionUID = 1L;
      String content;
      public Message(String content) {
         this.content = content;
      }
   }
}

Output

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

Header and object written to file.

Explanation

  • The stream starts with raw bytes (you could use them as a custom protocol header).

  • Then it writes a serialized Java object.

  • On deserialization, you'd read the header with InputStream.read() before calling readObject().

java_io_objectoutputstream.htm