Java - ObjectOutputStream write(byte[] buf, int off, int len) method



Description

The Java ObjectOutputStream write(byte[] buf, int off, int len) method writes a sub array of bytes.

Declaration

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

public void write(byte[] buf, int off, int len)

Parameters

  • buf − The data to be written.

  • off − The start offset in the data.

  • len − The number of bytes that are written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur.

Example - Usage of ObjectOutputStream write(byte[] buf, int off, int len) method

The following example shows the usage of ObjectOutputStream write(byte[] buf, int off, int len) 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', 'e', 'l', 'l', 'o'};
      
      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, 0, 3);

         // 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
         for (int i = 0; i < 3; i++) {
            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 −

hel

Example - Write a portion of a byte array to a file

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

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 = "partial_bytes1.bin";
      byte[] fullMessage = "HelloWorld".getBytes(); // 10 bytes

      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.write(fullMessage, 0, 5); // Writes "Hello"
         System.out.println("Wrote first 5 bytes: 'Hello'");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Optional: Read back and print
      try (FileInputStream fis = new FileInputStream(filename)) {
         long l = fis.skip(5);  
         byte[] data = fis.readAllBytes();
         String s1 = new String(data);
         String s2 = s1.substring(1, s1.length());

         System.out.println("Read from file: " + s2);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote first 5 bytes: 'Hello'
Read from file: Hello

Example - Write a middle slice of a byte array

The following example shows the usage of ObjectOutputStream write(byte[] buf, int off, int len) method. We're writing "loWo" (a slice from "HelloWorld" starting at index 3, length 4).

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 = "partial_bytes2.bin";
      byte[] message = "HelloWorld".getBytes(); // 10 bytes

      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.write(message, 3, 4); // Writes "loWo"
         System.out.println("Wrote 4 bytes from index 3: 'loWo'");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Optional: Read and print
      try (FileInputStream fis = new FileInputStream(filename)) {
         byte[] result = fis.readAllBytes();
         fis.skip(5);
         String s1 = new String(result);
         String s2 = s1.substring(6, s1.length());
         System.out.println("Read from file: " + s2);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote 4 bytes from index 3: 'loWo'
Read from file: loWo
java_io_objectoutputstream.htm
Advertisements