Android Binary File Read readResource(String resourceName)

Here you can find the source of readResource(String resourceName)

Description

Read a file from classpath as byte

Parameter

Parameter Description
resourceName file path and name, like "/somepath/somename.txt"

Return

file's data

Declaration

public static byte[] readResource(String resourceName) 

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 w w w .j a  va  2  s .  c  o m
     * Read a file from classpath as byte
     * @param resourceName file path and name, like "/somepath/somename.txt"
     * @return file's data
     */
    public static byte[] readResource(String resourceName) {
        byte[] bytes = null;
        try {
            InputStream ins = FileUtil.class
                    .getResourceAsStream(resourceName);
            bytes = new byte[ins.available()];
            ins.read(bytes);
            ins.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return bytes;
    }
    /**
     * 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. read(String fileName)
  3. readFile(File f)
  4. readFile(File file)
  5. readFile(File file, int size)