Java File to Byte Array getBytesFromFile(File file)

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

Description

Returns a byte stream from the given file.

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

byte[] File Bytes

Declaration

public static byte[] getBytesFromFile(File file) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.RandomAccessFile;

public class Main {
    /**/*from   w  w  w.  j a  v  a  2  s  .  co m*/
     * Returns a byte stream from the given file.
     * @param file
     * @return byte[] File Bytes
     */
    public static byte[] getBytesFromFile(File file) {
        byte[] bytesArray = null;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            bytesArray = new byte[(int) raf.length()];
            raf.read(bytesArray);
            raf.close();
        } catch (Exception e) {
            System.out.println("Random Access File failed.");
        }
        return bytesArray;
    }
}

Related

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