Java FileInputStream Read readFileBytes(File file)

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

Description

Returns the contents of file as a byte[].

License

Open Source License

Parameter

Parameter Description
file a File

Exception

Parameter Description
IOException if an error occurs

Return

a byte[]

Declaration

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

Method Source Code

//package com.java2s;
/* J_LZ_COPYRIGHT_BEGIN *******************************************************
* Copyright 2001-2011 Laszlo Systems, Inc.  All Rights Reserved.              *
* Use is subject to license terms.                                            *
* J_LZ_COPYRIGHT_END *********************************************************/

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

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

public class Main {
    /** Returns the contents of <var>file</var> as a byte[].
     * @param file a File//from  ww  w.  j ava  2s .  c  om
     * @return a byte[]
     * @throws IOException if an error occurs
     */
    public static byte[] readFileBytes(File file) throws IOException {
        InputStream istr = new FileInputStream(file);
        byte bytes[] = new byte[istr.available()];
        istr.read(bytes);
        istr.close();
        return bytes;
    }

    /**
     * Make sure you really want to ignore the exception
     * before using this.
     * @param in stream to close w/out throwing an exception
     */
    static public void close(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (Exception e) {
            }
        }
    }
}

Related

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