• Post Reply Bookmark Topic Watch Topic
  • New Topic

method to replace a char

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I know this question has been asked many times, but when I do a search, I get only old answers. And maybe, Java API 1.4 has come with some interestng features.
I wanted to know if it exists a convenient method to read an entire file and replace every occurence of a selected char by another char.
For e.g: I want; to replace all ; the semicolons; by comas.
I should get: I want, to replace all, the semicolons, by comas.
I know it is possible to hardcode a method doing it, but since there is many big files, I wanted to know if some efficient method exists.
Thx for any answers
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For programmer efficiency, I think the old ways are probably easiest. But if machine efficiency turns out to be important, you may find that the new classes in java.nio are useful. Check out this article. Offhand you might get good results using a MappedByteBuffer (especially since the replacement char is the same size as the original char). However beware that if you don't have a good understanding of the file encoding used in the ffiles you're dealing with, you can experience all sorts of problems. (E.g. what you think is a semicolon may really be the second byte of a Chinese character.) See also this recent discussion for more examples of java.nio classes, and the complications that can arise when looking at bytes directly rather than using Readers and Writers.
[ January 03, 2003: Message edited by: Jim Yingst ]
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you a lot Jim, those link are usefull to me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!