Java FileInputStream Read readFile(File file)

Here you can find the source of readFile(File file)

Description

Reads a file, and returns all bytes from that file.

License

Apache License

Parameter

Parameter Description
file - The file to read.

Return

A byte array containing every byte from the file.

Declaration

public static byte[] readFile(File file) 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

public class Main {
    /**/*from  w  w  w  .  ja  va  2 s  .  com*/
     * Reads a file, and returns all bytes from that file.
     * @param file - The file to read.
     * @return A byte array containing every byte from the file.
     */
    public static byte[] readFile(File file) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            byte[] d = new byte[in.available()];
            in.read(d);
            return d;
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        return null;
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)