Java - FileOutputStream write(byte[] b, int off, int len) method



Description

The Java FileOutputStream write(byte[] b, int off, int len) writes a portion of a byte array to an output file. Useful when you need to write only a portion of a byte array instead of the entire array.

Declaration

Following is the declaration for java.io.FileOutputStream.write(byte[] b, int off, int len) method −

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

Parameters

  • b − The source buffer.

  • off − The start offset in the data.

  • len − The number of bytes to write.

Return Value

This method does not return any value.

Exception

IOException− If an I/O error occurs.

Example - Usage of FileOutputStream write(byte[] b, int off, int len) method

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = null;
      FileInputStream fis = null;
      byte[] b = {65,66,67,68,69};
      int i = 0;
      char c;
      
      try {
         // create new file output stream
         fos = new FileOutputStream("test.txt");
         
         // writes byte to the output stream
         fos.write(b, 2, 3);
         
         // flushes the content to the underlying stream
         fos.flush();
         
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // prints
            System.out.print(c);
         }
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
         // closes and releases system resources from stream
         if(fos!=null)
            fos.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output

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

CDE

Example - Writing a Subset of a String

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      String data = "Hello, FileOutputStream!";

      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Convert string to byte array
         byte[] byteData = data.getBytes();

         // Write only "FileOutputStream" (starting from index 7, length 16)
         fos.write(byteData, 7, 16);

         System.out.println("Selected portion written to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Selected portion written to output.txt.

The contents of output.txt will be:

FileOutputStream

Explanation

  • The string "Hello, FileOutputStream!" is converted into a byte array.

  • write(byte[], 7, 16) writes only the substring "FileOutputStream" to output.txt

    • Offset 7− Start at the 7th index (F in "FileOutputStream").

    • Length 16− Write the next 16 characters.

Example - Writing a Subset of Binary Data

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      byte[] binaryData = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74}; // ASCII for "ABCDEFGHIJ"

      try (FileOutputStream fos = new FileOutputStream("binary_output.txt")) {
         // Write only "DEFGH" (from index 3, length 5)
         fos.write(binaryData, 3, 5);

         System.out.println("Subset of binary data written to binary_output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output()

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

Subset of binary data written to binary_output.txt.

Explanation

  • A byte array {65, 66, 67, 68, 69, 70, 71, 72, 73, 74} represents "ABCDEFGHIJ".

  • write(binaryData, 3, 5) writes only "DEFGH" to binary_output.txt

    • Offset 3 Start at D (ASCII 68).

    • Length 5 Write D, E, F, G, H.

  • The resulting file will contain: DEFGH

java_io_fileoutputstream.htm
Advertisements