Java Utililty Methods File to Byte Array

List of utility methods to do File to Byte Array

Description

The list of methods to do File to Byte Array are organized into topic(s).

Method

byte[]getBytesFromFile(File file)
Returns the contents of the file in a byte array.
FileInputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int) length];
try {
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
...
byte[]getBytesFromFile(File file)
get Bytes From File
InputStream is = new BufferedInputStream(new FileInputStream(file));
long length = file.length();
if (length > Integer.MAX_VALUE) {
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
...
byte[]getBytesFromFile(File file)
get Bytes From File
return getBytesFromStream(new FileInputStream(file), true);
byte[]getBytesFromFile(File file)
Returns a byte stream from the given 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.");
...
byte[]getBytesFromFile(File file)
get Bytes From File
byte[] b = new byte[(int) file.length()];
FileInputStream fs = new FileInputStream(file);
fs.read(b);
fs.close();
return b;
byte[]getBytesFromFile(File file)
Returns the contents of the file in a byte array.
byte[] result = null;
InputStream in = null;
try {
    in = new FileInputStream(file);
    long fileLength = file.length();
    if (fileLength > Integer.MAX_VALUE) {
        throw new IOException("File too large: " + file.getAbsolutePath());
    result = new byte[(int) fileLength];
    int offset = 0;
    int numRead = 0;
    while ((offset < result.length) && (numRead = in.read(result, offset, result.length - offset)) >= 0) {
        offset += numRead;
    if (offset < result.length) {
        throw new IOException("Could not completely read file:" + file.getAbsolutePath());
} finally {
    if (in != null) {
        in.close();
return result;
byte[]getBytesFromFile(File file)
get Bytes From File
byte[] bytes = null;
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
try {
    inputStream = new FileInputStream(file);
    outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    int len = 0;
...
byte[]getBytesFromFile(File file)
Extracts the bytes from a file.
FileInputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
try {
    byte[] buffer = new byte[4096];
    inputStream = new FileInputStream(file);
    outputStream = new ByteArrayOutputStream();
    int read;
    while ((read = inputStream.read(buffer)) != -1) {
...
byte[]getBytesFromFile(File file)
get Bytes From File
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
    throw new IOException("File is way too big");
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
...
byte[]getBytesFromFile(File inputFile)

This method reads a file and convert it into bytes

InputStream inputStream = new FileInputStream(inputFile);
long length = inputFile.length();
byte[] outputBytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
if (offset < outputBytes.length) {
    numRead = inputStream.read(outputBytes, offset, outputBytes.length - offset);
    while (offset < outputBytes.length && numRead >= 0) {
...