Java FileInputStream Read readFile(File path)

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

Description

read File

License

Open Source License

Declaration

private static byte[] readFile(File path) throws FileNotFoundException, IOException 

Method Source Code

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

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static byte[] readFile(File path) throws FileNotFoundException, IOException {
        int length = (int) path.length();
        byte[] content = new byte[length];
        InputStream inStream = new FileInputStream(path);
        try {//from   w  ww  .j  a va 2  s  .  c  om
            inStream.read(content);
        } finally {
            inStream.close();
        }
        return content;
    }

    private static byte[] read(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int nBytes = 0;
            while ((nBytes = input.read(buffer)) > 0) {
                output.write(buffer, 0, nBytes);
            }
            byte[] result = output.toByteArray();
            return result;
        } finally {
            try {
                input.close();
            } catch (IOException e) {

            }
        }
    }
}

Related

  1. readFile(File file, OutputStream output)
  2. readFile(File file, String encoding)
  3. readFile(File file, String encoding)
  4. readFile(File file, String encoding)
  5. readFile(File fyl)
  6. readfile(File path)
  7. readFile(File source)
  8. readFile(File src)
  9. readFile(File srcFile, OutputStream destStream)