Java FileInputStream Read readFileAtOnce(final File file)

Here you can find the source of readFileAtOnce(final File file)

Description

Reads the contents of the file at once and returns the byte array.

License

Open Source License

Parameter

Parameter Description
file a java.io.File object.

Exception

Parameter Description

Return

an array of byte.

Declaration

public static byte[] readFileAtOnce(final File file) throws IOException 

Method Source Code

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

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

import java.io.IOException;

public class Main {
    /**// ww w  .ja va2  s  .c om
     * Reads the contents of the file at once and returns the byte array.
     *
     * @param file a {@link java.io.File} object.
     * @throws java.io.IOException if any.
     * @return an array of byte.
     */
    public static byte[] readFileAtOnce(final File file) throws IOException {
        final FileInputStream fIn = new FileInputStream(file);
        return readFileAtOnce(fIn);
    }

    /**
     * Reads the contents of the file at once and returns the byte array.
     *
     * @param filename
     *            name of the file.
     * @throws java.io.IOException if any.
     * @return an array of byte.
     */
    public static byte[] readFileAtOnce(final String filename) throws IOException {
        final FileInputStream fIn = new FileInputStream(filename);
        return readFileAtOnce(fIn);
    }

    /**
     * Reads the contents of the file input stream. (Why not an InputStream btw?).
     * 
     * @param fIn
     * @return
     * @throws IOException
     */
    private static byte[] readFileAtOnce(final FileInputStream fIn) throws IOException {
        final byte[] ret = new byte[fIn.available()];
        fIn.read(ret);
        fIn.close();
        return ret;
    }
}

Related

  1. readFileAsInputStream(File file)
  2. readFileAsInputStream(String fileName)
  3. readFileAsProperties(File file)
  4. readFileAsResource(String fileName)
  5. ReadFileAsUtf8String(String filePath)
  6. readFileBinaries(File file)
  7. readFileBinary(String filename)
  8. readFileBytes(File file)
  9. readFileBytes(File file)