Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static ByteBuffer fromFile(File file) throws IOException { RandomAccessFile raf = null; FileChannel channel = null; try { raf = new RandomAccessFile(file, "r"); channel = raf.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()).load(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { // Ignored. } } if (raf != null) { try { raf.close(); } catch (IOException e) { // Ignored. } } } } }