Java FileInputStream Read readFileFully(File file)

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

Description

read File Fully

License

Open Source License

Declaration

public static byte[] readFileFully(File file) throws IOException 

Method Source Code


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

public class Main {
    private static final int BUFFER_SIZE = 4096;

    public static byte[] readFileFully(File file) throws IOException {

        byte[] buffer = new byte[BUFFER_SIZE];

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        FileInputStream inStream = null;

        try {/*w ww .  ja va 2s  .  com*/
            inStream = new FileInputStream(file);

            int readSize = 0;
            while ((readSize = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, readSize);
            }
        } finally {
            Arrays.fill(buffer, (byte) 0x00);
            buffer = null;
            buffer = outStream.toByteArray();

            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    //swallow here, just trying to close out
                }
            }

            try {
                outStream.close();
            } catch (IOException e) {
                //swallow here, just trying to close out
            }
        }

        return buffer;
    }
}

Related

  1. readFileBytes(String filename)
  2. readFileBytes(String fileName)
  3. readFileBytes(String filePath)
  4. readFileEndAsString(File file, int size_limit)
  5. readFileEx(String... file)
  6. readFileInByteArray(String aFileName)
  7. readFileRaw(File file)
  8. readFileToBinary(File lessCSS)
  9. readFileToByteArray(File file)