0

I have a program which writes out some data- I am using this logic

FileOutputStream outpre = new FileOutputStream(getfile());
FileChannel ch = outpre.getChannel();
ch.position(startwr);
ch.write(ByteBuffer.wrap(dsave));
outpre.close();

It writes out the correct data to the correct location, the only problem is that everything before the starting location to write (startwr) gets replaced with 00's, and the file is also changed making the point at which the writing was done, the end of the file.

How can I write data to the file without corrupting the previous data and changing the file size?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

2 Answers2

2

You need to instruct the stream to either append or overwrite the contents of the file...

Take a look at FileOutpuStream(File, boolean) for more details

Updated

After some mucking around, I found that the only solution I could get close to working was...

RandomAccessFile raf = null;

try {
    raf = new RandomAccessFile(new File("C:/Test.txt"), "rw");
    raf.seek(3);
    raf.writeBytes("BB");
} catch (IOException exp) {
    exp.printStackTrace();
} finally {
    try {
        raf.close();
    } catch (Exception e) {
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The Java API Says thus- "append - if true, then bytes will be written to the end of the file rather than the beginning" What does the API mean when it sais "bytes will be written to the end of the file rather than the begining" I don't understand how this will solve the issue Are you saying that if I change the code to FileOutputStream outpre = new FileOutputStream(getfile(), true); then the bug will go away? – Lucas Talarivera May 21 '13 at 05:22
  • Essentially it's saying, if `append` is `true` the data will be written to the end of the file, appending it to the file, otherwise the original content will be discard and the new data written to the file. Yes, if you use `FileOutputStream(getfile(), true)` you should find that anything you write to the file will be appended to the end of the file – MadProgrammer May 21 '13 at 05:26
  • Ok thank you, so far this method is working great (I would have never guessed to use RandomAccessFile) Also in the constructor for RandomAccessFile it uses (File, String) what is the importance of the string? – Lucas Talarivera May 21 '13 at 17:25
  • The second value determines the access mode, typically either "r" for read or "rw" for read/write – MadProgrammer May 21 '13 at 20:46
0

You can fix it this way

FileChannel fc = FileChannel.open(getFile().toPath(), StandardOpenOption.APPEND);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I just tried this and while it appends to the end of the file, the OP is trying to write to a specific location within the file, which I wasn't able to achieve using `FileChannel` in this way. I don't have much experience with `FileChannel`, so if you know of a way to seek to a file position and write, then please don't hesitate to fill us in :P – MadProgrammer May 21 '13 at 05:44