Home  >  Core Java

Java FilterOutputStream and FilterInputStream Example

By Arvind Rai, July 19, 2014
java.io.FilterOutputStream and java.io.FilterInputStream belong to JDK 1.0. These API associated with OutputStream and InputStream respectively.

FilterOutputStream

FilterOutputStream implements OutputStream and overrides its methods. Further FilterOutputStream has different sub classes to provide different type of functionality. Sub classes as BufferedOutputStream, DataOutputStream, DeflaterOutputStream etc.
FilterOutputStreamDemo.java
package com.cp.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
public class FilterOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		File file = new File("D:/cp/text.txt");
		FileOutputStream fos = new FileOutputStream(file);
		FilterOutputStream filterOS = new FilterOutputStream(fos);
                byte[] b = "Hello World!".getBytes();
	        for(int i=0;i<b.length;i++){
	    	  filterOS.write(b[i]);
	        }
	        fos.close();
	        System.out.println("Done");
	}
} 

FilterInputStream

FilterInputStream implements InputStream. Further FilterInputStream has different sub classes for different purpose as BufferedInputStream, DataInputStream, InflaterInputStream etc .
FilterInputStreamDemo.java
package com.cp.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class FilterInputStreamDemo {
	public static void main(String[] args) throws IOException {
		File file = new File("D:/cp/text.txt");
		FileInputStream  fis = new FileInputStream(file); 
		FilterInputStream filterIs = new BufferedInputStream(fis);
		int i =0;
		while((i=filterIs.read())!=-1){
			System.out.print((char)i);
		}
		fis.close();
		filterIs.close();
	}
} 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us