Android File to Byte Array Read readBytes(String path)

Here you can find the source of readBytes(String path)

Description

read Bytes

Declaration

public static final byte[] readBytes(String path) throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;

public class Main {
    public static final byte[] readBytes(InputStream in) throws IOException {
        byte[] ret = new byte[0];
        byte[] buf = new byte[2048];
        int len;//w  ww . j a v  a2s .c o  m
        for (;;) {
            len = in.read(buf);
            if (len > 0) {
                ret = Arrays.copyOf(ret, ret.length + len);
                System.arraycopy(buf, 0, ret, ret.length - len, len);
            } else {
                break;
            }
        }
        return ret;
    }

    public static final byte[] readBytes(String path) throws IOException {
        return readBytes(new File(path));
    }

    public static final byte[] readBytes(File file) throws IOException {
        FileInputStream in = new FileInputStream(file.getAbsolutePath());
        try {
            return readBytes(in);
        } catch (IOException e) {
            throw e;
        } finally {
            in.close();
        }
    }
}

Related

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