Android File to Byte Array Read file2byte(String path)

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

Description

filebyte

Declaration

public static byte[] file2byte(String path) 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

public class Main {

    public static byte[] file2byte(String path) {
        byte[] data = null;
        data = file2byte(new File(path));
        return data;
    }/*from  ww  w .j a  v  a  2  s. c  o  m*/

    public static byte[] file2byte(File file) {
        byte[] data = null;
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        } catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }
}

Related

  1. read(File file)
  2. buildDataFromFile(File file)
  3. buildDataFromFile(String path)
  4. buildDataFromFile(String path, int bufferSize)
  5. file2byte(File file)
  6. getByteFromFile(String fileName)
  7. getAvailableBytes(File root)
  8. getAvailableBytes(File root)
  9. getByteArrayFromSD(String path)