-2

Possible Duplicate:
Prepend lines to file in Java

I have a CSV-file which supports UTF-8 encoding. I want to insert some text in this file at starting point using java. How can I do this? I don't want to create another file and write that text and then contents of this file into new file.

Community
  • 1
  • 1
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38

1 Answers1

2

Simplest way is to create another file and write that text and then contain of existing file into new file. Then copy new file to the old filename replacing it. This has the added advantage that you can choose not to replace until the end.

An alternative for files that fit in memory would be to slurp it into memory and write it out to the same filename.

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141