Java File Read via ByteBuffer readFileAsByteArray(File file)

Here you can find the source of readFileAsByteArray(File file)

Description

read File As Byte Array

License

Open Source License

Declaration

public static byte[] readFileAsByteArray(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static byte[] readFileAsByteArray(File file) throws IOException {
        ByteBuffer bb;/*from  w  ww  .j a v a  2 s  . c  o  m*/
        FileChannel fc = null;
        try {
            fc = new FileInputStream(file).getChannel();
            bb = ByteBuffer.allocate((int) fc.size());
            fc.read(bb);
        } finally {
            if (fc != null)
                fc.close();
        }
        return bb.array();
    }
}

Related

  1. readFile(String name)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFileAsByteArray(String path)
  7. readFileAsStringArray(File file)
  8. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  9. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)