Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Define which characters are unwanted, then write code to remove them. A nice way to put it together would be to create your own subclass of FilterOutputStream, then chain it to get the results you want. E.g., OutputStream out = new MyOutputStream(new FileOutputStream("file.txt"));
I don't think you can do it directly in the stream. Otherwise, you have the String.trim() method which will strip leading and trailing whitespace or the String.replace (char oldChar, char newChar). You can also use the repalceAll() with a regex:
This says to replace all occurrences of one or more spaces with one space. HTH
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 ]
Gosh, all this arm-twisting just to prove Leslie wrong. I'm honored. As intended. Though I fibbed a bit here - there's also the fact that I just like using filter streams, and think their use should be more widely known. So I like to occasionally toss out a working example. But they are much less frequently needed now that Java finally has regexes... BTW, I did not suggest, I just said I think. Ah, close enough... But anyway, I can tell you're not the real Leslie Chaim, since you made no mention of the P-word for two whole consecutive posts.
But anyway, I can tell you're not the real Leslie Chaim, since you made no mention of the P-word for two whole consecutive posts. You're fuuny. Being obnoxious has it's limits Let's go back to the original questions by Chris: I am wondering how do you remove unwanted spaces in a output stream. I guess we're lucky the question was not:
Urgent!!! I need to remove unwanted spaces from a text file. How to do this in Java?
Well then I'd say do it in Perl: perl -pi -e 's/\s+/ /g' file.txt And if the unwanted spaces means any whitespace and across multiple lines just to something like this: perl -p0i -e 's/(\s)\1*/$1/g' file.txt These are just quick (and some say always dirty) examples. They may require some fine-tuning for actual use. I hope they are appealing enough to consider learning Perl. Sorry, Jim that I could not resist on this. (I was obnoxious after all ) On the other hand, I wanted to assure you no one is using my login name. Regards, Leslie [ May 21, 2003: Message edited by: Leslie Chaim ]