Java FileInputStream Read readFile(String path)

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

Description

read file to byte array

License

Open Source License

Parameter

Parameter Description
path a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] readFile(String path) throws IOException 

Method Source Code


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

import java.io.IOException;

public class Main {
    /**/*ww w.  j a  v a 2s  .c  om*/
     * read file to byte array
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static byte[] readFile(String path) throws IOException {
        final File file = new File(path);
        if (!file.exists())
            throw new IOException("the path" + path + " is not exists");
        final FileInputStream templateImageData = new FileInputStream(file);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] cache = new byte[255];
        int len = 0;
        while (-1 != (len = templateImageData.read(cache))) {
            outputStream.write(cache, 0, len);
        }
        outputStream.flush();
        outputStream.close();
        templateImageData.close();
        return outputStream.toByteArray();
    }
}

Related

  1. readFile(String name)
  2. readFile(String path)
  3. Readfile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String path)
  7. readFile(String path)
  8. readFile(String path)
  9. readFile(String path)