Java – Convert File to byte[]

This tutorial shows several ways to convert a File object to a byte[] array in Java.

1- Traditional way

The traditional conversion way is through using read() method of InputStream as the following:

2- Java NIO

With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO

Besides JDK, you can do the conversion using Apache Commons IO library in 2 ways:

3.1. IOUtils.toByteArray()

3.2. FileUtils.readFileToByteArray()


0 0 votes
Article Rating

Hussein Terek

Founder of, I have a passion in software engineering and everything related to java environment.

Inline Feedbacks
View all comments