Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing Strings on a text file..

 
John Ray Allen
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody please help me on how to read a string from a text file then change all of its occurence with a new string..for there are alot of "Dollar" string in a text file then I want to change it with "$". I find it hard to edit it on a text editor cause its so time consuming..the average capacity of my file is around 11Mb..I want to speed up the editing of my text file that's why I consult it to you guys..thanks in advance!
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, the easiest way to do that is with the unix utility 'sed', or with the find/replace functionlaity of most any text editor. If you really want to use Java, look at the regexp package, and I believe there's a String.replace method as well.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most editors (there are tons of fere editors out there for every platform) support a Replace All funtion. However, since we're here to learn about Java . . .

As Robert pointed out, you can read in the entire file to a StringBuffer and use indexOf()/replace() repeatedly or if you are using JDK 1.5, read it into a String and use replace() once. No need to regular expressions here since "Dollar" is literal.

The problem with that is that you gotta read in all 11MB at once. The plus side is that it's by far the simplest solution.

If you want to roll up your sleeves, the other solution is to modify the file in place (or write to a new file) using streams or the new I/O buffer/channel classes. Streams will probably be easier for this. You can modify the file in place only because your replacement string is shorter than the one you're replacing.

Isn't there a class like java.util.Scanner that simply does straight search-replace of streams and readers instead of parsing it into tokens?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most editors (there are tons of free editors out there for every platform) support a Replace All function. However, since we're here to learn Java . . .

As Robert pointed out, you can read in the entire file to a StringBuffer and use indexOf()/replace() repeatedly or if you are using JDK 1.5, read it into a String and use replace() once. No need for regular expressions since "Dollar" is a literal string.

The problem with that is that you have to read in the entire 11MB at once. The plus side is that it's by far the simplest solution (10-20 lines perhaps).

If you want to roll up your sleeves, the other solution is to modify the file in place (or write to a new file) using streams or the new I/O buffer/channel classes. Streams will probably be easier for this. You can modify the file in place because your replacement string is shorter than the one you're replacing.

Isn't there a class like java.util.Scanner that simply does straight search-replace of streams and readers instead of parsing it into tokens?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic