Java ByteBuffer Append appendToEnd(final File file, final ByteBuffer contents)

Here you can find the source of appendToEnd(final File file, final ByteBuffer contents)

Description

Append the specified byte buffer to the end of the specified file

License

Open Source License

Parameter

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

Declaration

private static final void appendToEnd(final File file, final ByteBuffer contents) 

Method Source Code

//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();
                }
            }
        }
    }
}

Related

  1. appendHex(StringBuilder sb, ByteBuffer bb)
  2. appendLong(long value, ByteBuffer buffer)
  3. appendOrRealloc(ByteBuffer dest, ByteBuffer toAppend)
  4. appendSurrogate(ByteBuffer bb, char c)
  5. appendToByteBuffer(ByteBuffer bb, byte[] bytes, int len)