Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update html file through java code

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeevi,

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

Thank you,

Rob
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i Agree..RandomAccessFile seems not to be the good choice
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic