Java File to Byte Array getBytesFromFile(String filename)

Here you can find the source of getBytesFromFile(String filename)

Description

Given name of file, return entire file as a byte array.

License

Open Source License

Parameter

Parameter Description
filename a parameter

Declaration

public static byte[] getBytesFromFile(String filename) 

Method Source Code

//package com.java2s;
/*//from  w  ww .j  a v  a 2 s.c om
 * $Id: PixelUtil.java,v 1.3 2007/01/29 09:08:46 eiki Exp $ Created on May 29,
 * 2006
 * 
 * Copyright (C) 2006 Idega Software hf. All Rights Reserved.
 * 
 * This software is the proprietary information of Idega hf. Use is subject to
 * license terms.
 */

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

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

public class Main {
    /**
     * Given name of file, return entire file as a byte array.
     * 
     * @param filename
     * @return
     */
    public static byte[] getBytesFromFile(String filename) {
        File f = new File(filename);
        byte[] bytes = null;
        if (f.exists()) {
            try {
                bytes = getBytesFromFile(f);
            } catch (Exception e) {
                System.out.println("getBytesFromFile() exception: " + e);
            }
        }
        return bytes;
    }

    /**
     * Given File object, returns the contents of the file as a byte array.
     */
    public static byte[] getBytesFromFile(File file) throws IOException {
        byte[] bytes = null;
        if (file != null) {
            InputStream is = new FileInputStream(file);
            long length = file.length();
            // Can't create an array using a long type.
            // Before converting to an int type, check
            // to ensure that file is not larger than Integer.MAX_VALUE.
            if (length > Integer.MAX_VALUE) {
                System.out.println("getBytesFromFile() error: File " + file.getName() + " is too large");
            } else {
                // Create the byte array to hold the data
                bytes = new byte[(int) length];
                int offset = 0;
                int numRead = 0;
                // Read in the bytes
                while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                    offset += numRead;
                }
                // Ensure all the bytes have been read in
                if (offset < bytes.length) {
                    throw new IOException(
                            "getBytesFromFile() error: Could not completely read file " + file.getName());
                }
            }
            // Close the input stream and return bytes
            is.close();
        }
        return bytes;
    }
}

Related

  1. getBytesFromFile(File file)
  2. getBytesFromFile(File file)
  3. getBytesFromFile(File inputFile)
  4. getBytesFromFile(final File file)
  5. getBytesFromFile(String file)
  6. getBytesFromFile(String fileName)
  7. getBytesFromFile(String filename)
  8. getBytesFromFile(String filename)
  9. getBytesFromFile(String filepath)