Java FilterWriter ClassLast Updated : 29 Jan 2026 Java FilterWriter class is an abstract class that is used to write filtered character streams. In this chapter, we will learn what the FilterWriter class is, why it is used, its declaration, fields, constructors, methods, and examples to understand how it works in Java I/O. What is FilterWriter Class in Java?The FilterWriter class is an abstract character output stream that wraps another Writer object to provide filtered or modified output. It is mainly used as a base class for creating custom writer classes. Subclasses of FilterWriter override one or more writing methods to modify the data before writing it to the underlying writer. Classes like BufferedWriter internally use this concept to add extra functionality. FilterWriter Class DeclarationThe FilterWriter class is a part of the java.io package and extends the Writer class. Fields of FilterWriter Class
Constructors of FilterWriter ClassThe FilterWriter class provides a protected constructor that is used by its subclasses. FilterWriter(Writer out)This constructor creates a FilterWriter object that wraps the specified Writer. Syntax: Here is the syntax: Note:Since FilterWriter is an abstract class, it cannot be instantiated directly. This constructor is used inside subclasses. Example of FilterWriter Class ConstructorThe following example demonstrates how a custom class extends FilterWriter and uses its constructor. Methods of FilterWriter ClassThe FilterWriter class provides the following commonly used methods:
Examples of Java FilterWriter ClassPractice the following examples to understand the concept and usage of the FilterWriter class. Example 1: Writing Filtered Data Using Custom FilterWriterThe following example demonstrates how to create a custom FilterWriter that converts text to lowercase before writing it to a file. Output: i love my country While running the current program if the current working directory does not contain the file, a new file is created and CustomFileWriter will write the text "I LOVE MY COUNTRY" in lowercase to the file. Example 2: Using FilterWriter to Modify Text Before Writing to FileThe following example demonstrates how a custom FilterWriter modifies data before writing it to a file. Here, the custom writer converts all characters to lowercase before storing them in the file. Output: java filterwriter second example Next TopicJava FilterReader class |
We request you to subscribe our newsletter for upcoming updates.