This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Update html file through java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Update html file through java code" Watch "Update html file through java code" New topic
Author

Update html file through java code

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
How Can i update an html file using java code.
suppose hi rancher is there in html file.
I will search if rancher is there and replace that with some xyz text in the same file.
How can i do that.
Ram Jeevi
Greenhorn

Joined: Nov 28, 2008
Posts: 7
Hi just try File, BufferedInputStream and BufferedOutputStream. While reading the file just check for the string and if equals write the other string which is to be replaced.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
No, new text will be written at end of file and will not be updated properly.Please tell me how we can update a file in java.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Hello Jeevi,

I think you missed our naming policy. Can you please change your name accordingly?

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
You can either try using a RandomAccessFile or (using the methods Jeevi suggested) you can read the file in and write the entire contents plus the changes out to a temporary file and then delete the old file and rename the new one.


Joanne
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
Note that RandomAccessFile will only work if the number of bytes you are replacing is exactly the same as the number of bytes you want to overwrite them with. If the numbers differ, the file will be garbage.


Ping & DNS - my free Android networking tools app
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Yes i Agree..RandomAccessFile seems not to be the good choice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update html file through java code
 
Similar Threads
need help returning an array, got an error
Read a html file into HTMLDocument
can add adtional metadata?
how to invoke a java class using javascript
Exam guides for SCWCD 1.5?