Topics Index
FilterOutputStream is the wrapper class for the Java OutputStream Class and it provides special features and improves the performance in writing to a file. FilterOutputStream overrides all the methods of OutputStream.

The subclasses of FilterOutputStream are Java BufferedOutputStream - BufferedOutputStream In Java, DataOutputStream, Java PrintStream Class
Constructor :
Constructor Description
protected FilterInputStream(InputStream in) Creates a FilterInputStream object for the passed Inputstream Object
Important Methods :
Method Description
void write(int b) Writes the specified byte to this output stream.
void flush() Flushes the output stream and forces any buffered output bytes to be written out to the stream.
void close() Closes this output stream and releases any system resources associated with the stream.

class FilterOutputStreamDemo
    public static void main(String arg[])
        File f = new File("E:/JavaPrograms/FolderOne/"); // LINE A
            System.out.println("File exists.");
            System.out.println("File not found.");
        FilterOutputStream fos = new FilterOutputStream(new FileOutputStream(f)); // LINE B
        String s = "I love programming.";
        byte[] b = s.getBytes(); // LINE C
        fos.write(b); // LINE D
        fos.close(); // LINE E

File exists.
I love programming.


At LINE A we are declaring the path of the file. At LINE B we are creating the FilterOutputStream for FileOutputStream object so that to improve the performance. At LINE C we are converting the string to byte array. At LINE D we are writing into File At LINE E we are closing the Stream object.

  • Now write I LOVE JAVA to file.
  • Comment the LINE E and run the program. See the file to check the data. No data will be present since the Stream is not closed the file no data will be shown.
Rest Of Methods :
Method Description
void write(byte[] b) Writes b.length bytes to the output stream.
void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to the output stream.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App