Here you can find the source of readFileToByteBuffer(File file)
static ByteBuffer readFileToByteBuffer(File file) throws IOException
//package com.java2s; //License from project: Apache License import java.io.*; import java.nio.ByteBuffer; public class Main { static ByteBuffer readFileToByteBuffer(File file) throws IOException { RandomAccessFile randomAccessFile = null; try {//from w w w .j a v a 2 s .co m randomAccessFile = new RandomAccessFile(file, "r"); byte[] bytes = new byte[(int) randomAccessFile.length()]; randomAccessFile.readFully(bytes); return ByteBuffer.wrap(bytes); } finally { if (randomAccessFile != null) randomAccessFile.close(); } } }