Java FileInputStream Read readFile(String path)

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

Description

Read whole data of a file.

License

Open Source License

Parameter

Parameter Description
path the path of a file.

Return

while data of a file on success, or null on failure.

Declaration

public static byte[] readFile(String path) 

Method Source Code

//package com.java2s;
/*************************************************************************************************
 * Class of utility methods/* www.ja v  a2 s  . com*/
 *                                                      Copyright (C) 2000-2006 Mikio Hirabayashi
 * This file is part of QDBM, Quick Database Manager.
 * QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License or any later version.  QDBM is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 * You should have received a copy of the GNU Lesser General Public License along with QDBM; if
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA.
 *************************************************************************************************/

import java.io.IOException;

import java.io.InputStream;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

public class Main {
    private static final int IOBUFSIZ = 8192;

    /**
     * Read whole data of a file.
     * @param path the path of a file.
     * @return while data of a file on success, or null on failure.
     */
    public static byte[] readFile(String path) {
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            is = new FileInputStream(path);
            baos = new ByteArrayOutputStream();
            byte[] buf = new byte[IOBUFSIZ];
            int len;
            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
        } catch (IOException e) {
            return null;
        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
            }
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
        }
        return baos.toByteArray();
    }
}

Related

  1. readFile(String imageName)
  2. readFile(String name)
  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)