Java FileInputStream Read readFile(File file)

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

Description

read a file and return file context as a byte[]

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/*//from  ww w.  ja  v a  2s  .  c  o m
 * @(#)FileUtil.java   
 *
 * Copyright (C) 2006-2008 www.interpss.com
 *
 * This program 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 (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * @Author Mike Zhou
 * @Version 1.0
 * @Date 01/30/2008
 * 
 *   Revision History
 *   ================
 *
 */

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

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

public class Main {
    /**
     * read a file and return file context as a byte[]
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] readFile(File file) throws IOException {
        InputStream inStream = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int n;
        byte[] buf = new byte[4096];
        do {
            n = inStream.read(buf);
            if (n >= 0)
                bos.write(buf, 0, n);
        } while (n > 0);
        inStream.close();
        return bos.toByteArray();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)