• 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

replace text in a file with new text

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers
i want to replace a text in a file with new text
how can i do it

thanx in advance

vijay
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you should try the Java in General Forum.
1) Work in memory and then write the file using DataOutput, OutputStream or Writer.
2) Directly into the file using RandomAccessFile.

If what you really want is to look for text to replace, (the question is a bit too simple) try the regular expression feature.

ps. c'mon, let's try searching the Javadoc or even google, plz.
[ March 21, 2006: Message edited by: Eiji Seki ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use RandomAccessFile..API

if ur Field Lengths are constant use a standard offset to move through records....using skipBytes method in RandomAccessFile. Then just write the bytes. using writeBytes() method
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, let's do this in Java In General (Beginner)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The times that RandomAccessFile will work for you are few and far between. You can only replace old content with new content of exactly the same length. Ok, it is possible to shift data around to accomodate length changes, but the complexity is high and the performance is questionable.

It's a lot more likely that you'll read the entire file, pass most content unchanged to a new file, identify and modify some content before writing it to the new file. Or if the size is manageable, read the entire file into memory, work on it in memory for a while, then rewrite the entire file.

Here's a favorite trick for rewriting a file that assures you always have a safe copy of the old and/or new data:

read original
write temp
rename original to backup
rename temp to original
erase backup
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic