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



Description

The Java FilterOutputStream write(byte[] b, int off, int len) method writes a specific portion of a byte array to the file. Writes len bytes, starting from the off index in the byte array. Prevents writing unnecessary parts of the array. Does not automatically flush (use flush() if needed). Overwrites the file unless opened in append mode.

Declaration

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

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

Parameters

  • b − source buffer to be written to the stream

  • 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 FilterOutputStream write(byte[] b, int off, int len) method

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

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

public class FilterOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      OutputStream os = null; 
      FilterOutputStream fos = null;
      FileInputStream fis = null;
      byte[] buffer = {65, 66, 67, 68, 69};
      int i = 0;
      char c;
      
      try {
         // create output streams
         os = new FileOutputStream("test.txt");
         fos = new FilterOutputStream(os);

         // writes buffer to the output stream
         fos.write(buffer, 2, 3);
                  
         // forces byte contents to written out to the stream
         fos.flush();
         
         // create input streams
         fis = new FileInputStream("test.txt");
         
         while((i = fis.read())!=-1) {
         
            // converts integer to the character
            c = (char)i;
            
            // prints
            System.out.println("Character read: "+c);
         }
         
      } catch(IOException e) {
         // if any I/O error occurs
         System.out.print("Close() is invoked prior to write()");
      } finally {
         // releases any system resources associated with the stream
         if(os!=null)
            os.close();
         if(fos!=null)
            fos.close();
      }
   }
}

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

Character read: C
Character read: D
Character read: E

Example - Writing a Subset of a String as Bytes

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

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

public class FilterOutputStreamDemo {
   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.

Explanation

  • Converts "Hello, FileOutputStream!" into a byte array.

  • Uses write(byteData, 7, 16) to write only "FileOutputStream".

  • The first 7 bytes ("Hello, ") are skipped.

Example - Writing a Subset of Binary Data

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

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

public class FilterOutputStreamDemo {
   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.bin")) {
         // Write only "DEFGH" (from index 3, length 5)
         fos.write(binaryData, 3, 5);

         System.out.println("Subset of binary data written to binary_output.bin.");
      } 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.bin.

Explanation

  • Creates a byte array {65, 66, 67, 68, 69, 70, 71, 72, 73, 74} (ASCII "ABCDEFGHIJ").

  • Calls write(binaryData, 3, 5), which writes only "DEFGH".

  • The first 3 bytes (ABC) are skipped.

java_io_filteroutputstream.htm
Advertisements