Here you can find the source of toByteArray3(String filename)
public static byte[] toByteArray3(String filename) throws IOException
//package com.java2s; import java.io.*; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { public static byte[] toByteArray3(String filename) throws IOException { FileChannel fc = null;/* ww w . j ava2s . co m*/ try { fc = new RandomAccessFile(filename, "r").getChannel(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load(); System.out.println(byteBuffer.isLoaded()); byte[] result = new byte[(int) fc.size()]; if (byteBuffer.remaining() > 0) { // System.out.println("remain"); byteBuffer.get(result, 0, byteBuffer.remaining()); } return result; } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } } }