Filterwriter

The FilterWriter class in Java is an abstract class that is utilized for writing filtered character streams.

Inheriting from the FilterWriter class involves overriding certain methods while potentially including extra methods and fields in the subclass.

Fields

Modifier Type Field Description
protected Writer out The underlying character-output stream.

Constructors

Modifier Constructor Description
protected FilterWriter(Writer out) It creates InputStream class Object

Methods

Modifier and Type Method Description
void close() It closes the stream, flushing it first.
void flush() It flushes the stream.
void write(char[] cbuf, int off, int len) It writes a portion of anarrayof characters.
void write(int c) It writes a single character.
void write(String str, int off, int len) It writes a portion of astring.

FilterWriter Example

Example

import java.io.*;
class CustomFilterWriter extends FilterWriter {
	CustomFilterWriter(Writer out) {
		super(out);
	}
	public void write(String str) throws IOException {
		super.write(str.toLowerCase());
	}
}
public class FilterWriterExample {
	public static void main(String[] args) {
		try {
			FileWriter fw = new FileWriter("Record.txt"); 
			CustomFilterWriter filterWriter = new CustomFilterWriter(fw);			
			filterWriter.write("I LOVE MY COUNTRY");
			filterWriter.close();
			FileReader fr = new FileReader("record.txt");
			BufferedReader bufferedReader = new BufferedReader(fr);
			int k;
			while ((k = bufferedReader.read()) != -1) {
				System.out.print((char) k);
			}
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

Output

i love my country

If the file is not found in the current working directory during the execution of the program, a new file will be generated. The CustomFileWriter class will then be utilized to write the phrase "I LOVE MY COUNTRY" in lowercase to this newly created file.

Input Required

This code uses input(). Please provide values below: