Java - FilterOutputStream write(int b) method



Description

The Java FilterOutputStream write(int b) method writes a single byte to the output stream. Only the lowest 8 bits (0-255) of the integer are written. The integer b is truncated to the last 8 bits (i.e., b & 0xFF). Useful for writing ASCII characters or binary data.

Declaration

Following is the declaration for java.io.FilterOutputStream.write(int b) method −

public void write(int b)

Parameters

  • b − The source byte.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterOutputStream write(int b) method

The following example shows the usage of Java FilterOutputStream write(int 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;
      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(65);
                  
         // forces byte contents to written out to the stream
         fos.flush();
         
         // create input streams
         fis = new FileInputStream("test.txt");
         
         // get byte from the file
         i = fis.read();
         
         // convert integer to character
         c = (char)i;
         
         // print character
         System.out.print("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: A

Example - Writing ASCII Characters to a File Using BufferedOutputStream

The following example shows the usage of Java FilterOutputStream write(int b) method.

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
         fos.write(72); // ASCII for 'H'
         fos.write(101); // ASCII for 'e'
         fos.write(108); // ASCII for 'l'
         fos.write(108); // ASCII for 'l'
         fos.write(111); // ASCII for 'o'

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

Characters written successfully to output.txt.

Explanation

  • Uses BufferedOutputStream, a subclass of FilterOutputStream.

  • Writes ASCII values 72 (H), 101 (e), 108 (l), 108 (l), 111 (o).

  • The file stores the characters as plain text.

Example - Writing Binary Data Using DataOutputStream

The following example shows the usage of Java FilterOutputStream write(int b) method.

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new DataOutputStream(new FileOutputStream("binary_output.bin"))) {
         fos.write(255); // 0xFF (last 8 bits stored)
         fos.write(128); // 0x80
         fos.write(64);  // 0x40

         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

  • Uses DataOutputStream, a subclass of FilterOutputStream.

  • Writes binary values (255 → 0xFF, 128 → 0x80, 64 → 0x40).

  • The file stores raw binary data instead of text.

java_io_filteroutputstream.htm
Advertisements