Java - FilterOutputStream write(byte[] b) method



Description

The Java FilterOutputStream write(byte[] b) method writes an entire byte array to the file at once. This is more efficient than writing one byte at a time. It writes all bytes from the given array to the file. It does NOT automatically flush (use flush() if necessary). It overwrites the file if not opened in append mode (true). It can be used to write text or binary data.

Declaration

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

public void write(byte[] b)

Parameters

b − source buffer to be written to the stream

Return Value

This method does not return any value.

Exception

  • IOException − If any I/O error occurs.

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

The following example shows the usage of Java FilterOutputStream write(byte[] b) 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);
                  
         // 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();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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

Character read: A
Character read: B
Character read: C
Character read: D
Character read: E

Example - Writing a String as Bytes to a File

The following example shows the usage of Java FilterOutputStream write(byte[] b) 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 byte array to the file
         fos.write(byteData);

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

Output

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

Data written successfully to output.txt.

Explanation

  • Converts a string into a byte array using .getBytes().

  • Creates a FileOutputStream for "output.txt".

  • Writes the byte array to the file.

  • The file will be overwritten if it already exists.

Example - Writing Binary Data (Image or File Content)

The following example shows the usage of Java FilterOutputStream write(byte[] b) 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}; // ASCII values of 'A', 'B', 'C', 'D', 'E'

      try (FileOutputStream fos = new FileOutputStream("binary_output.bin")) {
         // Writing byte array to the file
         fos.write(binaryData);

         System.out.println("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−

Binary data written to binary_output.bin.

Explanation

  • Creates a byte array {65, 66, 67, 68, 69}, which corresponds to "ABCDE".

  • Creates a FileOutputStream for "binary_output.bin".

  • Writes the byte array to the file.

  • The file stores raw binary data instead of text.

java_io_filteroutputstream.htm
Advertisements