Java File to Byte Array getBytes(File file)

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

Description

Load a file into a byte array

License

Open Source License

Parameter

Parameter Description
file the file to load

Exception

Parameter Description
IOException on errors

Return

byte[]

Declaration

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

Method Source Code

//package com.java2s;
/***************************************************************
 *  This file is part of the [fleXive](R) framework.
 *
 *  Copyright (c) 1999-2014/*from  ww w.  ja va2s  . c  o  m*/
 *  UCS - unique computing solutions gmbh (http://www.ucs.at)
 *  All rights reserved
 *
 *  The [fleXive](R) project is free software; you can redistribute
 *  it and/or modify it under the terms of the GNU Lesser General Public
 *  License version 2.1 or higher as published by the Free Software Foundation.
 *
 *  The GNU Lesser General Public License can be found at
 *  http://www.gnu.org/licenses/lgpl.html.
 *  A copy is found in the textfile LGPL.txt and important notices to the
 *  license from the author are found in LICENSE.txt distributed with
 *  these libraries.
 *
 *  This library 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.
 *
 *  For further information about UCS - unique computing solutions gmbh,
 *  please see the company website: http://www.ucs.at
 *
 *  For further information about [fleXive](R), please see the
 *  project website: http://www.flexive.org
 *
 *
 *  This copyright notice MUST APPEAR in all copies of the file!
 ***************************************************************/

import java.io.*;

public class Main {
    /**
     * Load a file into a byte array
     *
     * @param file the file to load
     * @return byte[]
     * @throws IOException on errors
     */
    public static byte[] getBytes(File file) throws IOException {
        InputStream is = null;
        if (file.length() > Integer.MAX_VALUE)
            throw new IOException("File " + file.getAbsolutePath()
                    + " is too large!");

        byte[] bytes = new byte[(int) file.length()];
        try {
            is = new FileInputStream(file);

            int curr = 0;
            int read;
            while (curr < bytes.length
                    && (read = is.read(bytes, curr, bytes.length - curr)) >= 0)
                curr += read;
            if (curr < bytes.length)
                throw new IOException("Failed to fully read "
                        + file.getAbsolutePath() + "!");
        } finally {
            if (is != null)
                is.close();
        }
        return bytes;
    }
}

Related

  1. getBytes(File file)
  2. getBytes(File file)
  3. getBytes(File file)
  4. getBytes(File file)
  5. getBytes(File file)
  6. getBytesFromFile(File f)
  7. getBytesFromFile(File file)
  8. getBytesFromFile(File file)
  9. getBytesFromFile(File file)