Android Binary File Read read(String fileName)

Here you can find the source of read(String fileName)

Description

Read data from file

Parameter

Parameter Description
fileName file's name

Return

file's data

Declaration

public static byte[] read(String fileName) 

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{
    /**/*  w w  w  .  j a  va2 s .  co m*/
     * 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(File file)
  2. readFile(File f)
  3. readFile(File file)
  4. readFile(File file, int size)
  5. readResource(String resourceName)