Core Java

7 Examples to Read File into a byte array in Java

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. 

1
byte[] filedata = IOUtils.toByteArray(new FileInputStream("info.xml"));

The  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 null.

2) Using Apache Commons FileUtils

The 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.

1
byte[] data = FileUtils.readFileToByteArray(new File("info.xml"));

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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
public static byte[] readFile(String file) throws IOException {
  
   File f = new File(file);
  
   // work only for 2GB file, because array index can only up to Integer.MAX
  
   byte[] buffer = new byte[(int)f.length()];
  
   FileInputStream is = new FileInputStream(file);
  
   is.read(buffer);
  
   is.close();
  
   return  buffer;
  
}

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).

1
byte[] bytes = Files.toByteArray(new File("info.xml"));

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.

1
byte[] g2Bytes = ByteStreams.toByteArray(new FileInputStream("info.xml"));

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:

1
2
Path path = Paths.get("info.xml");
byte[] raw = java.nio.file.Files.readAllBytes(path);

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.

1
2
3
4
5
6
7
RandomAccessFile f = new RandomAccessFile("info.xml", "rw");
  
byte[] b = new byte[(int)f.length()];
  
f.readFully(b);
  
  

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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
  
        InputStream in = null;
  
        try {
  
            in = openInputStream(file);
  
            return IOUtils.toByteArray(in, file.length());
  
        } finally {
  
            IOUtils.closeQuietly(in);
  
        }
  
    }

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.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.charset.Charset; 
import java.nio.charset.StandardCharsets; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import org.apache.commons.io.FileUtils; 
import org.apache.commons.io.IOUtils; 
import com.google.common.io.ByteStreams; 
import com.google.common.io.Files; 
   
/** 
 
 * @author Javin Paul 
 */
  
public class Testing {
  
    public static void main(String args[]) throws IOException {
  
        // Example 1: Using Apache Commons IOUtils to read file into byte array
  
        byte[] filedata = IOUtils.toByteArray(new FileInputStream("info.xml"));
  
        String str = new String(filedata, "UTF-8");
  
        System.out.println("File to byte[] using IOUtils.toByteArray \n" + str);
  
        // Example 2: Reading File to byte[] using FileUtils class
  
        byte[] data = FileUtils.readFileToByteArray(new File("info.xml"));
  
        System.out.println("Converting File to byte[] using FileUtils \n"
  
                + new String(data, StandardCharsets.UTF_8));
  
        // Example 3: Using FileInputStream and JDK
  
        byte[] contents = readFile("info.xml");
  
        System.out.printf("File to byte[] Java without thirdpaty library %n %s %n",
  
                new String(contents, StandardCharsets.UTF_8));
  
        // Example 4: Using Google Guava, uses NIO
  
        byte[] bytes = Files.toByteArray(new File("info.xml"));
  
        System.out.printf("Convert File to byte array Using Google %n %s %n",
  
                new String(bytes, "UTF-8"));
  
        // Example 5:
  
        byte[] g2Bytes = ByteStreams.toByteArray(new FileInputStream("info.xml"));
  
        System.out.println("File to byte[] using Guvava \n " + new String(g2Bytes, "UTF-8"));
  
        // Example 6: Using JDK 7 NIO Path and Files class
  
        Path path = Paths.get("info.xml");
  
        byte[] raw = java.nio.file.Files.readAllBytes(path);
  
        System.out.println("Read File to byte[] in JDK 7 \n " + new String(raw, "UTF-8"));
  
        //Example 7: Using RandomAccessFile in Java
  
        RandomAccessFile f = new RandomAccessFile("info.xml", "rw");
  
        byte[] b = new byte[(int) f.length()];
  
        f.readFully(b);
  
        System.out.println("Load File to byte[] in Java using RandomAccessFile \n "
  
                + new String(b, "UTF-8"));
  
    }
  
    /*
  
     * Reading File into byte array using JDK classes only
  
     */
  
    public static byte[] readFile(String file) throws IOException {
  
        File f = new File(file);
  
        // work only for 2GB file, because array index can only upto Integer.MAX
  
        byte[] buffer = new byte[(int) f.length()];
  
        FileInputStream is = new FileInputStream(file);
  
        is.read(buffer);
  
        is.close();
  
        return buffer;
  
    }
  
}
  
Output:
  
File to byte[] using IOUtils.toByteArray 
Name: Société Générale 
Headquarters: Île-de-France, France 
Converting File to byte[] using FileUtils 
Name: Société Générale 
Headquarters: Île-de-France, France 
File to byte[] Java without thirdpaty library
  
 Name: Société Générale 
Headquarters: Île-de-France, France
  
Convert File to byte array Using Google 
 Name: Société Générale
  
Headquarters: Île-de-France, France 
File to byte[] using Guvava
  
 Name: Société Générale 
Headquarters: Île-de-France, France
  
Read File to byte[] in JDK 7 
 Name: Société Générale
  
Headquarters: Île-de-France, France
  
Load File to byte[] in Java using RandomAccessFile 
 Name: Société Générale
  
Headquarters: Île-de-France, France

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.

Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: 7 Examples to Read File into a byte array in Java

Opinions expressed by Java Code Geeks contributors are their own.

Javin Paul

I have been working in Java, FIX Tutorial and Tibco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
gpay apk
2 years ago

thanks for this guidance bro…

brother status in marathi

thanks bro.. it is really very helpful ….

Back to top button