0

Is there an easy way to write to the top of a file with FileOutputStream?

This option

FileOutputStream fos = openFileOutput("Activity.log", Context.MODE_APPEND);

appends to the file (writes to the end). But I cannot find an "automatic" option to write to the beginning of the file instead. Is there one, or do I have to take it down a notch and calculate the start and end indexes etc. and write "bit for bit"?

  • 1
    Check [this][1] answer. Maybe it would be relevant for your question. [1]: http://stackoverflow.com/a/6127706/556337 – teoREtik Aug 22 '12 at 11:36
  • That would be overwriting, which isn't what I need. –  Aug 22 '12 at 11:51

1 Answers1

1

Create a temp file called "Activity_temp.log" and write the new data to it, then read the original "Activity.log" and append it to the temp file, then delete the "Activity.log" and rename the temp file to "Activity.log".

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • Blimey. Just don't append to the file! – user207421 Aug 22 '12 at 11:42
  • Yah, I was thinking about that solution. Maybe it's easier than messing around with the one file? –  Aug 22 '12 at 11:51
  • Well...if I don't append, I'll overwrite the contents of the file, won't I? I need to ADD it to the file, just not to the end but to the beginning. –  Aug 22 '12 at 11:53