aspose file tools*
The moose likes Java in General and the fly likes Text substitution in text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Text substitution in text files" Watch "Text substitution in text files" New topic
Author

Text substitution in text files

Grigory Ptashko
Greenhorn

Joined: Feb 21, 2003
Posts: 9
Hello.
My application must have a component for replacing text in text files, i.e. I give it the file, what to replace (old text and new text) and it gives me the file I want.
Also there must be some statistics. That is what replacements, how many times and where in the file they were made.
Does any one know something ready for this ?
Thanks in advance.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Grigory,
Welcome to JavaRanch. That really shouldn't be all that tough to accomplish. Your best bet is probably to wrap the file in a BufferedReader and read in a line at a time (assuming you don't want to change a newline) then parse the line for for old text and change it to new text. Each line could then be inserted into a List. As text is substituted you would update your statistics. If new text consists of multiple words then you will have to look ahead one or more lines.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Grigory Ptashko
Greenhorn

Joined: Feb 21, 2003
Posts: 9
I'm sorry if posted this question not to a proper forum.
But my problem is that I *want* to replace newlines.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I *want* to replace newlines.

Then the same basic architechture would apply. You would just need to look ahead as many lines as necessary to find your matches. You could even read the whole file into one big String which would make parsing much easier but might run the risk of an OutOfMemory error.
Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
Sounds like a job for a regular expression tool. Take a look at ORO http://jakara.apache.org or the regular expression classes in JDK 1.4
 
Consider Paul's rocket mass heater.
 
subject: Text substitution in text files