Android File to Byte Array Read file2byte(File file)

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

Description

filebyte

Declaration

public static byte[] file2byte(File file) 

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;
    }//  w ww.jav a2  s .  co  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. readFromFile(String fileName, int offset, int len)
  2. read(File file)
  3. buildDataFromFile(File file)
  4. buildDataFromFile(String path)
  5. buildDataFromFile(String path, int bufferSize)
  6. file2byte(String path)
  7. getByteFromFile(String fileName)
  8. getAvailableBytes(File root)
  9. getAvailableBytes(File root)