Hello guys, Java programmers often face scenarios in real-world programming, where they need to load data from a file into a byte array, it could be text or binary file. One example is to convert the contents of a file into String for display. Unfortunately, Java’s File class, which is used to represent both files and directories, doesn’t have a method say
toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn’t allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
If you are a fan of Apache commons and Google Guava like me, then you may already familiar with one-liner code, which can quickly read a file into a byte array; if not, then this is the right time to explore those API.
In this tutorial, we are going to see 7 different examples to read File to a byte array, some by using third party libraries, and others by using JDK 6 and JDK 7 core Java libs.
Depending upon your choice, you can use any of the following methods to convert file data into bytes. One thing to keep in mind is what you are doing with byte array; if you are creating String from a byte array, then beware with character encoding. You may need to find out correct character encoding by reading metadata information like Content-Type of HTML pages and of XML documents.
While reading XML documents, it’s a bad idea to first read an XML file and store it in a String. Instead, it’s better to pass
InputStream to XML parsers, and they will figure out the encoding themselves correctly.
One more thing to note is that you cannot read file larger than 2GB into a single byte array, you need multiple byte arrays for that. This limitation comes from the fact that the array index in Java is of int type, whose maximum value is
2147483647, roughly equivalent to 2GB.
Btw, I am expecting that you are familiar with basic Java Programing and Java API in general.
7 ways to read a file into a byte array in Java
Without wasting any more of your time, here are all the seven ways to load a file into a byte array in Java:
1) Using Apache Commons IOUtils
This is one of the easiest ways to read a file data into a byte array, provided you don’t hate third-party libraries. It’s productive because you don’t need to code it from scratch, worrying about exception handling, etc.
IOUtils.toByteArray(InputStream input) Gets the contents of an
InputStream as a byte. This method also buffers the input internally, so there is no need to use a
BufferedInputStream, but it’s not null-safe. It throws
NullPointerException if the input is
2) Using Apache Commons FileUtils
FileUtils class from
org.apache.commons.io package provides a general file manipulation facility like writing to a file or reading from a file. This method is used to read the contents of a file into a byte array, and the good thing about this is that the file is always closed.
3) Using FileInputStream and JDK
This is the classic way of reading the file’s content into a byte array. Don’t forget to close the stream once done. Here is the code to read a file into a byte array using FileInputStream class in Java:
In production, use finally block to close streams to release file descriptors.
4) Using Google Guava Files class
Files class of Google Guava provides utility methods for working with files, like converting files to a byte array, to string with specified charset, copy, move, etc.
Files.toByteArray() method reads all bytes from a file into a byte array and throws
IllegalArgumentException if the file size is bigger than the largest possible byte array (2^31 – 1).
This approach of reading files content into byte array has several advantages, first of all, you don’t need to reinvent the wheel. Second, it uses NIO for reading a file, which will perform better than stream IO. You also don’t need to worry about handling exceptions and closing streams, as Guava does for you.
5) Using Guava’s ByteStreams utility
Guava’s ByteStreams class provides utility methods for working with byte arrays and I/O streams. The
toByteArray() takes an InputStream and reads all bytes into a byte array but it does not close the stream, so you need to close it by yourself.
This is one reason, I don’t prefer this method, the Java 7 example we saw in the last section takes care of closing streams.
By the way, If you are using Java in-memory constraint environment like
Android, then consider using obfuscator like proguard to remove unused classes from third-party libraries. For example, Guava by default adds more than 2MB to an APK. But with Proguard it comes down to about 250KB
6) Using JDK 7 NIO Files and Path
If you are using Java 7, then this is the best way to convert File into a byte array. It allows you to read all bytes from a file and capture them in a byte array. All you need to know is the path of the file.
Here is the code sample to read a file in Java 7:
The biggest advantage of this approach is that it doesn’t require any third-party libraries. It’s also a static method, which makes it very convenient. It also ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Something Java has been lacking from the first edition.
By the way, this method is only intended for simple use where it is convenient to read all bytes into a byte array. It is not intended for reading large files and throws OutOfMemoryError, if an array of the required size cannot be allocated, for example, the file is larger than 2GB.
By the way, if you only have File object and not Path then you can also use
File.toPath() to convert File to Path in JDK 1.7.
7) Using RandomAccessFile in Java
You can also use RandomeAccessFile to convert File into an array of bytes as shown below, though you can also use
read(byte) method, it’s better to use readFully.
Also, note that RandomAccessFile is not thread-safe. So, synchronization may be needed in some cases.
Last thing, some of the code here is not production quality, as they are not handling exceptions properly. In real-world, all file handling code must close streams in finally block to release file descriptor associated with that, failure to do so may result in you java.io.IOException: Too many open files error.
Sometimes you can expect libraries like Apache commons IO for closing streams properly, as seen below from a code snippet from
FileUtils class of Apache Commons IO, the
closeQuietly() methods close a stream ignoring nulls and exceptions.
but it’s not always true, as Google Guava’s
ByteStreams.toByteArray method doesn’t close the stream. It’s better to check documentation before using a particular method in production code. In general, it’s better to use JDK API if available and that’s why a good knowledge of JDK goes a long way to becoming an expert Java programmer.
Java Program to Read A file into Byte Array in Java
Here is our complete Java program to read a file into a byte array in Java. This combines all the 6 approaches I have shown above. You can copy-paste this example and run in your favorite IDE like Eclipse, NetBeans, or IntelliJIDEA.
That’s all on this tutorial of 7ways to read a file into a byte array in Java. Now you know that there are multiple ways to read the file in Java, some by using third party libraries like Apache Commons IO, Google Guava, Apache MINA, and others by just employing standard JDK file input-output classes. Depending upon your requirement, you can use any of these solutions to read file data into a byte in Java. Keep an eye on character encoding if you are converting byte array to String.
Also, remember that array in Java can only hold a limited amount of data as it’s length cannot exceed
Integer.MAX_VALUE (2GB). So you cannot convert a large file into a single-byte array, though you can read large data using input stream, you need to process them in chunks or using multiple byte arrays.
If you like this article and want to learn more about improved file IO in recent Java version, please check the following tutorials:
- The Complete Java Developer RoadMap (guide)
- 3 ways to read a file line by line in Java 8 (examples)
- 10 Courses to learn Java in 2020 (courses)
- How to read a text file line by line using BufferedReader in Java? (answer)
- 15 things Java Programmers can learn in 2020 (article)
- How to use a memory-mapped file in Java? (answer)
- Top 5 Skills to Crack Coding Interviews (skills)
- How to read an XML file as String in Java? (tutorial)
- How to read/write Excel (both XLS and XLSX) files in Java using Apache POI? (tutorial)
- 2 ways to parse CSV file in Java? (answer)
- How to delete a directory with files in Java? (answer)
- How to parse XML file in Java using SAX parser? (guide)
- How to convert JSON to Object in Java? (example)
- How to read XML file in Java using JDOM parser? (tutorial)
- How to parse a big JSON file using Jackson Streaming API? (example)
- How to read a file in one line in Java 8? (example)
- How to copy File in Java? (example)
- How to generate MD5 checksum for file in Java? (solution)
- How to read/write RandomAccessFile in Java? (example)
Thanks for reading this article so far. If you find this Java File tutorial useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.