Java FileInputStream Read readFile(final File file)

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

Description

Read file.

License

Apache License

Parameter

Parameter Description
file File

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

byte[]

Declaration

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

Method Source Code

//package com.java2s;
/*//from  w w w. jav a2 s.c  o m
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

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

public class Main {
    /**
     * Read file.
     *
     * @param file
     *            File
     * @return byte[]
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static byte[] readFile(final File file) throws IOException {
        return readStream(new FileInputStream(file));
    }

    /**
     * Read stream.
     *
     * @param inStream
     *            InputStream
     * @return String
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static byte[] readStream(final InputStream inStream) throws IOException {
        DataInputStream in = null;
        try {
            in = new DataInputStream(inStream);
            final int size = in.available();
            final byte arr[] = new byte[size];
            in.readFully(arr);
            return arr;
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

Related

  1. readFile(File source)
  2. readFile(File src)
  3. readFile(File srcFile, OutputStream destStream)
  4. readFile(File target)
  5. readFile(FileInputStream fileinputstream, byte abyte0[])
  6. readFile(final File file)
  7. readFile(final File file)
  8. readFile(final InputStream stream)
  9. readFile(final String fileNamePath)