File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes how to append in a file from beginning. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to append in a file from beginning." Watch "how to append in a file from beginning." New topic

how to append in a file from beginning.

saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
hello all,
i am doing some file related work at some point i want to append the file from the beginning but i don't know how to do this, please help.

i m using File Writer constructor with second argument true for appending in the file but it appends from the end of the file but i want to insert something at beginning of the file contents.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

You can't, or at least not directly. File systems usually don't allow prepending, only appending. You'll need to use a temporary file as an intermediate solution. Write to the temporary file first, then delete the original file and rename the temporary file to the original file.

How To Ask Questions How To Answer Questions
wood burning stoves
subject: how to append in a file from beginning.
It's not a secret anymore!