Android File Read readFileToByteArray(String pathToFile)

Here you can find the source of readFileToByteArray(String pathToFile)

Description

read File To Byte Array

License

Open Source License

Declaration

public static byte[] readFileToByteArray(String pathToFile) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {

    public static byte[] readFileToByteArray(String pathToFile) {
        byte[] result = null;

        try {//from   ww w  .j ava2 s.  co  m
            File file = new File(pathToFile);

            if (file.length() <= Integer.MAX_VALUE) {
                result = new byte[(int) file.length()];
                @SuppressWarnings("resource")
                RandomAccessFile raf = new RandomAccessFile(file, "r");

                raf.readFully(result);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. readTemplate(String filePath)
  2. readTextFile(File file)
  3. loadFile(String str)
  4. getFileContent(File file)
  5. LoadFile(String fileName, Resources resources)