• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text substitution in text files

 
Grigory Ptashko
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Grigory Ptashko
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic