Java.io - SPLessons

Java.io FilterOutputStream

Home > Lesson > Chapter 27
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FilterOutputStream

Java.io FilterOutputStream

shape Introduction

The Java.io FilterOutputStream is the class used to write the bytes which are filtered. The filter output stream is subclass to the output stream class and overrides all the methods from the output stream class. In filter output stream class also have its default methods and have the additional functionality called filter.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io FilterOutputStream class is declared below: Public class FilterOutputStream extends OutputStream

shape Fields

Java.io FilterOutputStream class fields are:
  • Protected OutputStream out : The field represents the current stream have to filter.

Class Constructors

shape Table

Constructor Description
Protected FilterOutputStream(Out putStream out) The function of this constructor is to create the filer output stream instance and used to write based on the supplied argument.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void write (int b) The function of this method is to write the represented byte into output stream.
void flush () The function of this method is to flush the specified output stream.
void write (byte[] b, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of bytes.

Inherited Methods

shape Description

From the following classes, methods are inherited to the filter output stream class.
  • Java.io.object
  • Java.io.OutputStream

shape Examples

Usage of void write (byte[] b, int off, int n) method. [c]import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; public class FilterOutputStream { public static void main(String[] args) throws Exception { OutputStream os = null; FilterOutputStream fos = null; FileInputStream fis = null; byte[] buffer = {83, 80, 76, 69, 83, 83, 79, 78, 83}; int i=0; char c; try{ // Creating a new output stream instance os = new FileOutputStream("D://test.txt"); fos = new FilterOutputStream(os); // It writes buffer to the output stream fos.write(buffer, 2, 3); // forces byte contents for to written to the stream fos.flush(); // creating a new input stream fis = new FileInputStream("D://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 all the system resources associated with this stream if(os!=null) os.close(); if(fos!=null) fos.close(); } } }[/c] Output The result will be as follows. [c]Character read: L Character read: E Character read: S [/c] Usage of void write (int b) method. [c]import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; public class FilterOutputStream { public static void main(String[] args) throws Exception { OutputStream os = null; FilterOutputStream fos = null; FileInputStream fis = null; int i=0; char c; try{ // creating a new output stream os = new FileOutputStream("D://test.txt"); fos = new FilterOutputStream(os); // writes buffer to the output stream fos.write(83); // forces byte contents for to written out to the stream fos.flush(); // creating a new input streams fis = new FileInputStream("D://test.txt"); // To get byte from the file i = fis.read(); // To convert integer to character c = (char)i; // To 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 all the system resources associated with this stream if(os!=null) os.close(); if(fos!=null) fos.close(); } } }[/c] Output The result will be as follows. [c]Character read: S[/c]

Summary

shape Key Points

  • The Java.io FilterOutputStream is the class used to write the bytes which are filtered.
  • The filter output stream is subclass to the output stream class and overrides all the methods from the output stream class.
  • In filter output stream class also have its default methods and have the additional functionality called filter.