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[]fileToBytes(File file)
file To Bytes
FileInputStream fin = null;
byte arr[] = new byte[(int) file.length()];
try {
    fin = new FileInputStream(file);
    fin.read(arr);
} catch (IOException e) {
    throw new RuntimeException(e.getMessage(), e);
} finally {
...
byte[]fileToBytes(File file)
file To Bytes
ByteArrayOutputStream fileBytes = new ByteArrayOutputStream((int) file.length());
InputStream fileIn = new FileInputStream(file);
try {
    byte buffer[] = new byte[1024];
    int length;
    while ((length = fileIn.read(buffer)) > 0) {
        fileBytes.write(buffer, 0, length);
} finally {
    fileIn.close();
return fileBytes.toByteArray();
byte[]fileToBytes(File path)
Serialize a file into bytes.
FileInputStream fin = new FileInputStream(path);
byte[] buffer = new byte[(int) fin.getChannel().size()];
try {
    if (fin.read(buffer) == -1) {
        throw new IOException("File " + path.getAbsolutePath() + " is empty");
} finally {
    fin.close();
...
byte[]fileToBytes(File source)
Reads the contents of a file as a byte[].
FileInputStream fIn = null;
try {
    fIn = new FileInputStream(source);
    byte[] bytes = new byte[(int) source.length()];
    int read = 0;
    while (read < bytes.length) {
        int thisRead = fIn.read(bytes, read, bytes.length - read);
        if (thisRead == -1) {
...
byte[]fileToBytes(final File f)
file To Bytes
return streamToBytes(new FileInputStream(f));
byte[]getBytes(File aFile)
Returns bytes for a file.
if (aFile == null)
    return null;
if (!aFile.exists())
    return null;
if (!aFile.canRead())
    return null;
if (aFile.isDirectory())
    return null;
...
byte[]getBytes(File archiveFile)
DOC amaumont Comment method "getBytes".
long length = archiveFile.length();
int lengthFinalSize = 0;
if (length > Integer.MAX_VALUE) {
    throw new IllegalStateException("Capacity is over !");
} else {
    lengthFinalSize = (int) length;
FileInputStream fis = new FileInputStream(archiveFile);
...
byte[]getBytes(File contentFile)
get Bytes
InputStream is = null;
try {
    is = new FileInputStream(contentFile);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buf = new byte[BUFFER_SIZE];
    int read;
    while ((read = is.read(buf)) > 0) {
        os.write(buf, 0, read);
...
byte[]getBytes(File f)
get Bytes
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] result = getBytes(fis);
fis.close();
return result;
byte[]getBytes(File f)
get Bytes
byte[] b = new byte[(int) f.length()];
FileInputStream fis;
try {
    fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
try {
    fis.read(b);
    return b;
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
return null;