• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Text substitution in text files

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic