Android Utililty Methods File to Byte Array Read

List of utility methods to do File to Byte Array Read

Description

The list of methods to do File to Byte Array Read are organized into topic(s).

Method

byte[]readFileToByteArray(String filePath)
read File To Byte Array
if (StringUtil.isEmpty(filePath)) {
    return null;
File file = new File(filePath);
return readFileToByteArray(file);
byte[]fileToBytes(String filePath)
file To Bytes
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
    FileInputStream in = new FileInputStream(file);
    ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
    byte[] cache = new byte[CACHE_SIZE];
    int nRead = 0;
    while ((nRead = in.read(cache)) != -1) {
...
byte[]fileToByte(String filePath)
file To Byte
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
    FileInputStream in = new FileInputStream(file);
    ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
    byte[] cache = new byte[CACHE_SIZE];
    int nRead = 0;
    while ((nRead = in.read(cache)) != -1) {
...
byte[]getBytesFromFile(File file)
get Bytes From File
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
...
byte[]readBytes(File file)
read Bytes
FileInputStream in = new FileInputStream(file.getAbsolutePath());
try {
    return readBytes(in);
} catch (IOException e) {
    throw e;
} finally {
    in.close();
byte[]readBytes(String path)
read Bytes
return readBytes(new File(path));
byte[]readFromFile(String fileName, int offset, int len)
read From File
if (fileName == null) {
    return null;
File file = new File(fileName);
if (!file.exists()) {
    Log.i(TAG, "readFromFile: file not found");
    return null;
if (len == -1) {
    len = (int) file.length();
Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len
        + " offset + len = " + (offset + len));
if (offset < 0) {
    Log.e(TAG, "readFromFile invalid offset:" + offset);
    return null;
if (len <= 0) {
    Log.e(TAG, "readFromFile invalid len:" + len);
    return null;
if (offset + len > (int) file.length()) {
    Log.e(TAG, "readFromFile invalid file len:" + file.length());
    return null;
byte[] b = null;
try {
    RandomAccessFile in = new RandomAccessFile(fileName, "r");
    b = new byte[len]; 
    in.seek(offset);
    in.readFully(b);
    in.close();
} catch (Exception e) {
    Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
    e.printStackTrace();
return b;
byte[]read(File file)
read
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
    byte[] buffer = new byte[4096];
    ous = new ByteArrayOutputStream();
    ios = new FileInputStream(file);
    int read = 0;
    while ((read = ios.read(buffer)) != -1) {
...
byte[]buildDataFromFile(File file)
build Data From File
return buildDataFromFile(file.getAbsolutePath());
byte[]buildDataFromFile(String path)
build Data From File
return buildDataFromFile(path, 1024);