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[]getBytes(File f)
get Bytes
FileInputStream is = new FileInputStream(f);
byte[] res = new byte[(int) f.length()];
is.read(res);
is.close();
return res;
byte[]getBytes(File file)
Reads a file into a byte array.
FileInputStream stream = new FileInputStream(file);
try {
    long length = file.length();
    if (length > Integer.MAX_VALUE)
        throw new IOException("File too big: " + file.getName());
    int ilength = (int) length;
    byte[] bytes = new byte[ilength];
    int alength = stream.read(bytes);
...
byte[]getBytes(File file)
get Bytes
try (FileInputStream stream = new FileInputStream(file)) {
    return readBytes(stream, (int) file.length());
byte[]getBytes(File file)
get Bytes
if (file == null || !file.exists())
    return null;
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    byte[] tmp = new byte[4096];
    InputStream is = new BufferedInputStream(new FileInputStream(file));
    while (true) {
        int r = is.read(tmp);
...
byte[]getBytes(File file)
Returns the byte[] rawValue of the given File.
InputStream in = new FileInputStream(file);
int fileSize = (int) file.length();
if (fileSize > Integer.MAX_VALUE) {
    in.close();
    throw new IOException("File size to large: " + fileSize + " > " + Integer.MAX_VALUE);
byte[] bytes = new byte[fileSize];
int offset = 0;
...
byte[]getBytes(File file)
get Bytes
if (file == null || !file.exists()) {
    return null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream in = new FileInputStream(file);
int c = in.read();
while (c != -1) {
    out.write(c);
...
byte[]getBytes(File file)
get Bytes
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int n = 0;
while ((n = fileInputStream.read(bytes)) != -1) {
    byteArrayOutputStream.write(bytes, 0, n);
byteArrayOutputStream.close();
...
byte[]getBytes(File file)
get Bytes
if (!file.exists()) {
    throw new FileNotFoundException();
if (file.length() > Integer.MAX_VALUE) {
    throw new IOException("file is too large for single read");
byte[] bytes = new byte[(int) file.length()];
try (FileInputStream fileInputStream = new FileInputStream(file)) {
...
byte[]getBytes(File file)
get Bytes
Preconditions.checkNotNull(file);
Preconditions.checkArgument(file.exists());
int size = (int) file.length();
byte[] bytes = new byte[size];
InputStream in = new FileInputStream(file);
try {
    int response = in.read(bytes);
    Preconditions.checkState(response == size);
...
byte[]getBytes(File file)
Load a file into a byte array
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;
...