Here you can find the source of appendToEnd(final File file, final ByteBuffer contents)
Parameter | Description |
---|---|
file | the file to append the byte buffer to |
contents | the byte buffer append to the end of the file. pre-condition: the byte buffer is at its start position post-condition: the byte buffer has been rewound |
private static final void appendToEnd(final File file, final ByteBuffer contents)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { /** Append the specified byte buffer to the end of the specified file * @param file the file to append the byte buffer to * @param contents the byte buffer append to the end of the file. * pre-condition: the byte buffer is at its start position * post-condition: the byte buffer has been rewound */// w w w .j a v a2 s. co m private static final void appendToEnd(final File file, final ByteBuffer contents) { FileChannel fileChannel = null; try { fileChannel = new FileOutputStream(file, true).getChannel(); // Set the file channel's position to the beginning or end depending on the specified parameter fileChannel.write(contents); contents.rewind(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileChannel != null) { try { fileChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } } }