Java - OutputStream write(byte[] b) method



Description

The Java OutputStream write(byte[] b) method writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length)

Declaration

Following is the declaration for java.io.OutputStream.write(byte[] b) method.

public void write(byte[] b)

Parameters

b − The data.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of OutputStream write(byte[] b) method

The following example shows the usage of OutputStream write(byte[] b) method.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class OutputStreamDemo {
   public static void main(String[] args) {
      byte[] b = {'h', 'e', 'l', 'l', 'o'};
      
      try {
         // create a new output stream
         OutputStream os = new FileOutputStream("test.txt");

         // craete a new input stream
         InputStream is = new FileInputStream("test.txt");

         // write something
         os.write(b);

         // read what we wrote
         for (int i = 0; i < b.length; i++) {
            System.out.print("" + (char) is.read());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

hello

Example - Writing a byte array to a file using FileOutputStream

The following example shows the usage of OutputStream write(byte[] b) method.

OutputStreamDemo.java

package com.tutorialspoint;

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

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("output1.txt");

         String data = "This is an example using write(byte[] b)";
         byte[] byteArray = data.getBytes();  // Convert string to byte array

         fos.write(byteArray);  // Write entire byte array to the file
         fos.close();

         System.out.println("Data written to output1.txt");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written to output1.txt

Explanation

  • getBytes() converts a string into a byte array.

  • write(byte[] b) writes the entire byte array to the file output1.txt.

  • Efficient for writing strings or binary data at once.

Example - Writing part of a byte array using write(byte[] b) (with ByteArrayOutputStream)

The following example shows the usage of OutputStream write(byte[] b) method.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();

         byte[] data = {65, 66, 67, 68, 69};  // ASCII for A, B, C, D, E

         baos.write(data);  // Writes all bytes to memory buffer

         byte[] result = baos.toByteArray();
         for (byte b : result) {
            System.out.print((char) b + " ");
         }

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

Output

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

A B C D E

Explanation

  • ByteArrayOutputStream is used to write data into a byte array in memory.

  • write(byte[] b) stores the bytes into the internal buffer.

  • toByteArray() retrieves the written data.

  • This is useful when you're preparing data before writing it to a file or sending it over a network.

java_io_outputstream.htm
Advertisements