Android File Read readFileToByte(File file)

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

Description

read File To Byte

Declaration

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

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {

    public static byte[] readFileToByte(File file) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(
                (int) file.length());
        BufferedInputStream in = null;
        try {//from  w  ww . j  ava 2  s . c o m
            in = new BufferedInputStream(new FileInputStream(file));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                bos.close();
            }
        }
    }
}

Related

  1. read(File path, int i)
  2. readFile(File file)
  3. readFile(String filepath)
  4. readFileAsString(String filePath)
  5. readFileToString(String fileName)
  6. readFileToString(String pathToFile)
  7. readFromFile(String filename)
  8. readInstallationFile(File installation)