Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import android.util.Log;

public class Main {
    private static final String TAG = "Util";

    public static byte[] readBytesFromFile(String path) {
        if (path == null || path.trim().length() == 0) {
            return null;
        }

        File file = new File(path);

        if (file.exists() && file.isFile()) {
            int filelength = (int) file.length();
            byte[] filecontent = new byte[filelength];
            try {
                FileInputStream in = new FileInputStream(file);
                in.read(filecontent);
                in.close();

            } catch (IOException e) {
                Log.v(TAG, "read file is error");
                return null;
            }
            return filecontent;
        }
        return null;
    }

    public static byte[] readBytesFromFile(File file) {
        if (file != null && file.exists() && file.isFile()) {
            int filelength = (int) file.length();
            byte[] filecontent = new byte[filelength];
            try {
                FileInputStream in = new FileInputStream(file);
                in.read(filecontent);
                in.close();

            } catch (Exception e) {
                Log.v(TAG, "read file is error");
                return null;
            }
            return filecontent;
        }
        return null;
    }
}