Android Binary File Read read(File file)

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

Description

Read data from file

Parameter

Parameter Description
file file to be read

Return

file's data

Declaration

public static byte[] read(File file) 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main{
    /**//from ww  w.  j  a v  a  2s .c  om
     * Read data from file
     * @param fileName file's name
     * @return file's data
     */
    public static byte[] read(String fileName) {
        byte[] bytes = null;
        try {
            if (StringUtil.isEmpty(fileName)) {
                return null;
            }
            InputStream in = new BufferedInputStream(new FileInputStream(
                    fileName));
            bytes = new byte[in.available()];
            in.read(bytes);
            in.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return bytes;
    }
    /**
     * Read data from file
     * @param file file to be read
     * @return file's data
     */
    public static byte[] read(File file) {
        byte[] bytes = null;
        try {
            InputStream ins = new BufferedInputStream(new FileInputStream(
                    file));
            bytes = new byte[ins.available()];
            ins.read(bytes);
            ins.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return bytes;
    }
}

Related

  1. read(String fileName)
  2. readFile(File f)
  3. readFile(File file)
  4. readFile(File file, int size)