Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append to begining of file

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

Is there any easy way I can append a string to the begining of a file ? I know a coupla ways but not happy with them..

1. I can read the entire file and then create a new file but this time inserting a new string at the begining. This is not a good soln bcos the file is a huge log.

2. Using RandomAccessFile, I can insert at the begining but it rather does a replace than an insert.

appreciate any suggestions,

cheers,
 
As far as I know your solution 1 is the only one which will work. However, if the reason you want to avoid it is because to read the file would involve a huge memory overhead, then create a new file with the inserted string first, read the old file chunk by chunk, and append to the new file. Finally, delete the old file and rename the new to the old.


Mark [openup]
 
could you please give a small code example ?

thnx
 
Here is some pseudo code you will "need to fill the blanks in" - it will help you learn ...

Code:
File originalFile = 
FileInputStream fis  = 
byte[] bytes = new byte[(int)originalFile.length()];
fis.read(bytes);
fis.close();
FileOutputStream fos = // overwrite your orignal file here
fos.write("myline".getBytes());
fos.write(bytes);
//close everything ....

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi,

Appears I hv almost found a breathrough..

1. the File copy is working great
BUT
I have found a better way to achieve my task and hv run into a small problem. I could insert a blank line at the top of LOG via log4j. Now, I am accessing the file using RandomAccessFile to write my string but am running into a small problem (i hope!).

Whats happening is that , RAF is writing my string ok to the first blank line but is eating away part of my data in the second line!!! whys this happening ??

Heres my RAF code:

RandomAccessFile raf = new RandomAccessFile(nm,"rw");
raf.seek(0);
raf.writeBytes("USER;ACTION;DIALOG");
// raf.writeBytes("\n"); // inserting this line is not helping anyway...
raf.close();

My original file:

// <- this is the blank line
sands;testing;cude;munde
sands;testing;cude;munde

My RAF modified file : as u can c some data is missing...

USER ACTION DIALOG
e munde
sands testing cude munde


What am i doing wrong !?
thnx,
 
Looks like it's still overwriting rather than inserting text in the file. I think Custom24 is right about your solution 1 being the only one which will work. A RandomAccessFile will allow you to write at any point in the file, but it will overwrite whatever's already there; the file won't grow unless you append at the end of the file.
My suggestion would be to modify sedj's code to loop and read the original file in blocks until it reaches the end of the file.
 
Does your blank line contains spaces characters? For your idea to work, it has to have enough space characters that allow the text you want to add to overwrite to.
 
U were right about it byam.. All I had to do was insert more %n and voila, its working grt..

thnx guys..
 
Hi there,

Follow Sandeepmur's idea, I also found an easy way to do the same thing:
Stringbuffer whatever you'd like to inster at the top of file.
Read in the firstline of the file and append to your stringbuffer.

RAF can overwrite the firstline by your new stringbuffer.

Am i right?

Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top