Java File to Byte Array getBytesFromFile(File file)

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

Description

get Bytes From File

License

Open Source License

Declaration

public static byte[] getBytesFromFile(File file) 

Method Source Code


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

import java.io.*;

public class Main {
    public static byte[] getBytesFromFile(File file) {

        byte[] buffer = null;
        BufferedInputStream bis = null;

        InputStream fis = null;//  www . j  ava  2s  .  co  m
        try {

            if (file.exists()) {
                fis = new FileInputStream(file.getAbsolutePath());
                bis = new BufferedInputStream(fis);

                int len = (int) file.length();
                int bytesRead = 0;
                buffer = new byte[len];
                if ((bytesRead = bis.read(buffer, 0, len)) == -1) {
                    System.err.println("wrong");
                }
                //                bis.read(buffer);

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null)
                    bis.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }
}

Related

  1. getBytesFromFile(File file)
  2. getBytesFromFile(File file)
  3. getBytesFromFile(File file)
  4. getBytesFromFile(File file)
  5. getBytesFromFile(File file)
  6. getBytesFromFile(File file)
  7. getBytesFromFile(File file)
  8. getBytesFromFile(File file)
  9. getBytesFromFile(File file)