Java FileInputStream Read readFile(String path)

Here you can find the source of readFile(String path)

Description

Reads file and return the byte Array

License

Apache License

Parameter

Parameter Description
path a parameter

Declaration

public static byte[] readFile(String path) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    /**// ww  w.  j  a v a 2s  .  c om
     * Reads file and return the byte Array
     * 
     * @param path
     * @return
     */
    public static byte[] readFile(String path) {
        File file = new File(path);
        byte[] fileData = new byte[(int) file.length()];
        DataInputStream dis;
        try {
            dis = new DataInputStream((new FileInputStream(file)));

            dis.readFully(fileData);
            dis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return fileData;
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String path, long offset)
  7. readFile(String path, Properties store)
  8. readFile(String path, String encoding)
  9. readFile(String path, String root, OutputStream out)