wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes replace text in a file with new text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replace text in a file with new text" Watch "replace text in a file with new text" New topic
Author

replace text in a file with new text

kotha vijaybabu
Ranch Hand

Joined: Jan 28, 2006
Posts: 77
hi ranchers
i want to replace a text in a file with new text
how can i do it

thanx in advance

vijay
Eiji Seki
Ranch Hand

Joined: Feb 15, 2006
Posts: 88
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 ]

SCJD URLyBird (WIP)<br />SCJP 1.5
Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
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


Animesh Saxena<br /> <br />Open Source Developer
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, let's do this in Java In General (Beginner)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replace text in a file with new text
 
Similar Threads
Text substitution in text files
how to replace text in a file
Java File Error - Format Issue
How to seek start and end pointers without reading a file
how to insert data into existing file