<< How can I create a maintenance window for my web app? | Home | Are JDBC statements portable across databases? >>

Filtering characters from a Reader

It's an occasionally legitimate question: "How do I filter content from a Reader?" There're related questions for Streams, too. Here's an example of a simple Reader class that shows how to remove specific characters from a Reader: FilterCharacterReader.java.

I/O in Java (well, normal I/O) is based on the decorator pattern. A reader or stream does the basic gruntwork of getting characters from a source - a URL, a file, whatever - and then other wrapper classes provide additional functionality like buffering input, counting lines, what have you.

This class (which is not intended to be fully functional!) scans the input and removes characters that have been added to its ignore list. There's a sample main() that creates a StringReader and then removes the 'a' from the input.

Just in case it's necessary: Readers are for text data, Streams are for binary. There are exceptions, of course, but let's pretend they don't exist as they're outliers anyway.

Have fun.




Add a comment Send a TrackBack