• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Changing Strings on a text file..

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic