Java AbstractCharacterFilterReader-class And Method Code Example

Here is an example of how to use the AbstractCharacterFilterReader class from the Apache Commons IO library in Java:


public class MyCharacterFilterReader extends AbstractCharacterFilterReader {
    public MyCharacterFilterReader(Reader in) {

    protected int processChar(int c) throws IOException {
        if (c == 'a') {
            return 'b';
        return c;

In this example, MyCharacterFilterReader is a custom implementation of AbstractCharacterFilterReader that replaces all occurrences of the letter 'a' with the letter 'b' when reading from the input Reader.

Here's how you can use it:

Reader input = new StringReader("abc");
Reader filtered = new MyCharacterFilterReader(input);
int character;
while ((character = != -1) {
    System.out.print((char) character);

This will output: "bbc"

Please note that this is a simple example and in real-world usage, you might want to do more complex processing on the characters before returning them.