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) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static byte[] getBytesFromFile(File file) throws IOException {
        byte[] bytes = null;
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {/*  w w w. ja v a2  s. c  o m*/
            inputStream = new FileInputStream(file);
            outputStream = new ByteArrayOutputStream();

            byte[] buffer = new byte[256];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            bytes = outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
        }
        return bytes;
    }
}

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 inputFile)
  9. getBytesFromFile(final File file)