Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing in the beginning of a text file Java

Tags:

java

file

text

I need to write something into a text file's beginning. I have a text file with content and i want write something before this content. Say i have;

Good afternoon sir,how are you today?
I'm fine,how are you?
Thanks for asking,I'm great

After modifying,I want it to be like this:

Page 1-Scene 59
25.05.2011

Good afternoon sir,how are you today?
I'm fine,how are you?
Thanks for asking,I'm great

Just made up the content :) How can i modify a text file like this way?

like image 353
toruko Avatar asked May 25 '11 16:05

toruko


1 Answers

You can't really modify it that way - file systems don't generally let you insert data in arbitrary locations - but you can:

  • Create a new file
  • Write the prefix to it
  • Copy the data from the old file to the new file
  • Move the old file to a backup location
  • Move the new file to the old file's location
  • Optionally delete the old backup file
like image 97
Jon Skeet Avatar answered Oct 21 '22 22:10

Jon Skeet