Sometimes it's necessary to remove certain characters from text files. One of the most efficient ways to do this is to do it when the file is being read. The following class does this by implementing a FilterReader. All that's necessary is to override isExcluded. A sample override is given below and the source is available HERE

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


/**
 * Subclasses should implement isExcluded to determine
 * which characters will be 'swallowed' by the Reader
 *
 @author CEHJ
 * @version 1.0
  */
public abstract class ExclusionReader extends FilterReader {
    public ExclusionReader(Reader in) {
        super(in);
    }

    // For testing only
    public static void main(String[] args) throws IOException {
        String s = null;

        if (args.length > 0) {
            s = args[0];
        } else {
            s = "h\000\r\ti\ndd\001en\t";
        }

        Reader in = new ExclusionReader(new StringReader(s)) {
                final String INCLUSIONS = "\t\r\n";

                public boolean isExcluded(int i) {
                    System.out.println("isExcluded called");

                    char c = (char) i;
                    boolean isControl = Character.isISOControl(c);
                    System.out.printf("isControl is %b for char %04x\n",
                        isControl, i);

                    return isControl && (INCLUSIONS.indexOf(c) < 0);
                }
            };

        int b = -1;

        int charsRead = -1;
        final int BUF_SIZE = 8;
        char[] buf = new char[BUF_SIZE];

        while ((charsRead = in.read(buf)) > -1) {
            System.out.print(new String(buf, 0, charsRead));
        }

        in.close();
    }

    public abstract boolean isExcluded(int c);

    public int read() throws IOException {
        int c = super.read();

        if (c < 0) {
            return c;
        } else if (isExcluded(c)) {
            return read();
        } else {
            return c;
        }
    }

    public int read(char[] b) throws IOException {
        return read(b, 0, b.length);
    }

    public int read(char[] buf, int off, int len) throws IOException {
        if (len <= 0) {
            if (len < 0) {
                throw new IndexOutOfBoundsException();
            } else if ((off < 0) || (off > buf.length)) {
                throw new IndexOutOfBoundsException();
            }

            return 0;
        }

        int charsRead = 0;
        int c = read();

        while ((c > -1) && (charsRead < len)) {
            buf[off + charsRead++] = (char) c;
            c = read();
        }

        return (charsRead > 0) ? charsRead : (-1);
    }
}