Java File to Byte Array fileToByteArray(File file)

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

Description

Reads the contents of a file in to a byte array.

License

Apache License

Parameter

Parameter Description
file file to read

Exception

Parameter Description
IOException throw if there is a problem reading the file in to the byte array

Return

the byte contents of the file

Declaration

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

Method Source Code

//package com.java2s;
/*// w  w  w .j  a v a 2  s  .  co  m
 * Copyright 2005 University Corporation for Advanced Internet Development, Inc.
 *
 * 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.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    /**
     * Reads the contents of a file in to a byte array.
     * 
     * @param file file to read
     * @return the byte contents of the file
     * 
     * @throws IOException throw if there is a problem reading the file in to the byte array
     */
    public static byte[] fileToByteArray(File file) throws IOException {
        long numOfBytes = file.length();

        if (numOfBytes > Integer.MAX_VALUE) {
            throw new IOException("File is to large to be read in to a byte array");
        }

        byte[] bytes = new byte[(int) numOfBytes];
        FileInputStream ins = new FileInputStream(file);
        int offset = 0;
        int numRead = 0;
        do {
            numRead = ins.read(bytes, offset, bytes.length - offset);
            offset += numRead;
        } while (offset < bytes.length && numRead >= 0);

        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }

        ins.close();
        return bytes;
    }
}

Related

  1. FiletoByteArray(File file)
  2. fileToByteArray(File file)
  3. fileToByteArray(File file)
  4. fileToByteArray(File file)