File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Changing Strings on a text file.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing Strings on a text file.." Watch "Changing Strings on a text file.." New topic
Author

Changing Strings on a text file..

John Ray Allen
Ranch Hand

Joined: Jan 06, 2005
Posts: 50
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

Joined: Jun 23, 2004
Posts: 172
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.


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Aug 07, 2003
Posts: 1646
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing Strings on a text file..
 
Similar Threads
How to check StringArray is sorted or not?
can we typecast String to String array
How to change font of String?
a problem with my program..
how to string search ?