Well, Leslie's replaceAll() suggestion is probably easiest. But since she/he (darn unisex names) suggests it can't be done with streams, I feel I must offer a counterexample:
We could also replace the (c == ' ') check with something like Character.isWhitespace(c), to convert
all consecutive whitespace to a single space. Or do something to convert spaces and tabs, but not newlines. Many possiblities. I'd say the replaceAll() approach will generally be more powerful and flexible; the only reasons to use a FilterWriter are (a) to get slightly better efficiency (I think) - no need to create & destroy a bunch of
String objects, and (b) to prove Leslie wrong.
Note that yes, this is pretty much what Greg suggested previously - except the moment you talk about "characters" rather than "bytes", it's probably best to use a Writer or Reader rather than InputStream or OutputStream.
Incidentally for efficiency you'd usually want to put a BufferedWriter immediately inside the SpaceTrimWriter, e.g.
Writer w = new SpaceTrimWriter(new BufferedWriter(new FileWriter("test.txt")));
This way all those individual write(char) calls get converted into a few big write(char[], int, int) calls before you get to the FileWriter, which works much better. Bulk methods are preferred whenever possible.
[ May 20, 2003: Message edited by: Jim Yingst ]