The Artima Developer Community
Sponsored Link

Java Answers Forum
I can't append to a file, what a shame !!!

2 replies on 1 page. Most recent reply: Sep 29, 2006 12:24 AM by Mohan Kumar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
M Adawi

Posts: 9
Nickname: adawi
Registered: Mar, 2006

I can't append to a file, what a shame !!! Posted: Sep 24, 2006 1:08 PM
Reply to this message Reply
Advertisement
Yes Yes I know about FileWriter(File, boolen append), It is not a problem to append on the end of the file,

1- But how to append into the middle of it ?

for example a text file includes 7 chars 1235678, I want to append just after 3 the char 4, what I should do

Do I have to read the file until I find '3' then I write to a temp file, append to its end , and continue reading what after '3' then append again to the end of the temp file, then create a new file with the same name to overwrite the old original one, read the tem file, write all data to that file,

even with the use of BufferReader/Writer, it is painfull and unefficient, in case I am right,

2-do you know why sun did that, I don't think they miss it out.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: I can't append to a file, what a shame !!! Posted: Sep 25, 2006 12:06 AM
Reply to this message Reply
You got the point.

INSERTING something in the middle of a file has nothing to do with APPENDING.

Depending on the size of th file you could copy the whole content into a new file making the modifications, then delete the old file and rename the new file.
This could cause of course problems with disc space.
So you could also append let's say a empty string to the file, then shift the filecontent beginning at the end until you reach the point where you want to insert the new content.

Mohan Kumar

Posts: 3
Nickname: itsmohankm
Registered: Sep, 2006

Re: I can't append to a file, what a shame !!! Posted: Sep 29, 2006 12:24 AM
Reply to this message Reply
Try java.io.RandomAccessFile -> seek() routine. Must solve your issue.

Flat View: This topic has 2 replies on 1 page
Topic: pseduo code help Previous Topic   Next Topic Topic: Sound Off: JSF, STRUCTS, & J2EE

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use