Android File to Byte Array Read read(File file)

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

Description

read

Declaration

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

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static byte[] read(File file) throws IOException {

        ByteArrayOutputStream ous = null;
        InputStream ios = null;//w  w  w .ja  va2  s  .  co m

        try {
            byte[] buffer = new byte[4096];
            ous = new ByteArrayOutputStream();
            ios = new FileInputStream(file);
            int read = 0;
            while ((read = ios.read(buffer)) != -1) {
                ous.write(buffer, 0, read);
            }
        } finally {
            try {
                if (ous != null)
                    ous.close();
            } catch (IOException e) {
            }

            try {
                if (ios != null)
                    ios.close();
            } catch (IOException e) {

            }
        }

        return ous.toByteArray();
    }
}

Related

  1. fileToByte(String filePath)
  2. getBytesFromFile(File file)
  3. readBytes(File file)
  4. readBytes(String path)
  5. readFromFile(String fileName, int offset, int len)
  6. buildDataFromFile(File file)
  7. buildDataFromFile(String path)
  8. buildDataFromFile(String path, int bufferSize)
  9. file2byte(File file)