o7planning

Java FilterReader Tutorial with Examples

  1. FilterReader
  2. Examples

1. FilterReader

FilterReader is an abstract subclass of Reader class, which is the base class to create subclasses to selectively read required characters. For example, you want to read a HTML document, and ignore tags. You need to write a subclass of FilterReader, you cannot use FilterReader directly because it is an abstract class.
FilterReader does not directly read data from the origin (eg file) but it manages another Reader that is responsible for reading data from the origin. FilterReader selectively processes the data obtained from the Reader it manages.
A look at the source code of FilterReader class shows: All the methods that it inherits from the parent class have been overridden to act as a delegate of the Reader object it manages:
FilterReader class
package java.io;

public abstract class FilterReader extends Reader {
    protected Reader in;
 
    protected FilterReader(Reader in) {
        super(in);
        this.in = in;
    }
    public int read() throws IOException {
        return in.read();
    }
    public int read(char cbuf[], int off, int len) throws IOException {
        return in.read(cbuf, off, len);
    }
    public long skip(long n) throws IOException {
        return in.skip(n);
    }  
    public boolean ready() throws IOException {
        return in.ready();
    }
    public boolean markSupported() {
        return in.markSupported();
    }
    public void mark(int readAheadLimit) throws IOException {
        in.mark(readAheadLimit);
    }
    public void reset() throws IOException {
        in.reset();
    }
    public void close() throws IOException {
        in.close();
    }
}
FilterReader​ constructors
protected FilterReader​(Reader in)

2. Examples

Example: Write a subclass of FilterReader to read HTML text but ignore tags.
RemoveHtmlTagReader.java
package org.o7planning.filterreader.ex;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;

public class RemoveHtmlTagReader extends FilterReader {

    private boolean intag = false;

    public RemoveHtmlTagReader(Reader in) {
        super(in);
    }

    // We override this method.
    // The principle will be:
    // Read only characters outside of the tags.
    @Override
    public int read(char[] buf, int from, int len) throws IOException {

        int charCount = 0;

        while (charCount == 0) {
            charCount = super.read(buf, from, len);
            if (charCount == -1) {
                // Ends of
                return -1;
            }
            int last = from;

            for (int i = from; i < from + charCount; i++) {
                // If not in tag
                if (!this.intag) {
                    if (buf[i] == '<') {
                        this.intag = true;
                    } else {
                        buf[last++] = buf[i];
                    }
                } else if (buf[i] == '>') {
                    this.intag = false;
                }
            }
            charCount = last - from;
        }
        return charCount;
    }

    // Also need to override this method.
    @Override
    public int read() throws IOException {
        char[] buf = new char[1];
        int result = read(buf, 0, 1);
        if (result == -1) {
            return -1;
        } else {
            return (int) buf[0];
        }
    }
}
RemoveHtmlTagTest.java
package org.o7planning.filterreader.ex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
 
public class RemoveHtmlTagTest {
 
    public static void main(String[] args) throws IOException {
        // Create a Reader.
        Reader in = new StringReader("<h1>Hello \n <b>World</b><h1>");
 
        RemoveHtmlTagReader filterReader = new RemoveHtmlTagReader(in);
        BufferedReader br = new BufferedReader(filterReader);
 
        String s = null;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
        br.close();
    }
}
Output:
Hello
 World

Java IO Tutorials

Show More