• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing unwanted characters from output

 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering how do you remove unwanted spaces in a output stream. any Ideas
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"));
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gosh, all this arm-twisting just to prove Leslie wrong.
I'm honored.
BTW, I did not suggest, I just said I think.
For what it's worth, I understand streams a bit more now.
Thanks Jim,
Leslie
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, Jim that I could not resist on this. (I was obnoxious after all )
No problem; I'm much the same in this respect.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is vintage!
Programmer to Programmer
Ciao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic