Java Read File to Byte Array

In Java, readind a file to byte array may be needed into various situations. For example, passing the information through the network as well as other APIs for further processing.

Let’s learn about a few ways of reading data from files into a byte array in Java.

1. Files.readAllBytes() – Java 8

Files.readAllBytes() is best method if you are using Java 7, 8 and above.

Example 1: Reading file to byte[] in Java 8

public class ContentToByteArrayExample
{
   public static void main(String[] args)
   {
      Path path = Paths.get("C:/temp/test.txt");
      byte[] data = Files.readAllBytes(path);
   }
}

Read More: 3 ways to read files using Java NIO

2. FileInputStream – Java 6

Use java.io.FileInputStream for reading the content of a file in Java 6.

Example 2: Reading a file byte by byte

import java.io.File;
import java.io.FileInputStream;

public class ContentToByteArrayExample
{
   public static void main(String[] args)
   {
      
      File file = new File("C:/temp/test.txt");
      
      readContentIntoByteArray(file);
   }

   private static byte[] readContentIntoByteArray(File file)
   {
      FileInputStream fileInputStream = null;
      byte[] bFile = new byte[(int) file.length()];
      try
      {
         //convert file into array of bytes
         fileInputStream = new FileInputStream(file);
         fileInputStream.read(bFile);
         fileInputStream.close();
         for (int i = 0; i < bFile.length; i++)
         {
            System.out.print((char) bFile[i]);
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      return bFile;
   }
}

3. FileUtils, IOUtils – Apache Commons IO

Another good way to read data into a byte array is in apache commons IO library.

Example 3: Reading an entire file into byte[]

//Using FileUtils.readFileToByteArray()
byte[] org.apache.commons.io.FileUtils.readFileToByteArray(File file)

//Using IOUtils.toByteArray
byte[] org.apache.commons.io.IOUtils.toByteArray(InputStream input) 

4. Files, ByteStreams – Guava

Another good way to read data into byte array is in Google Guava library.

Example 4: Reading an entire file into byte[]

//Using Files.toByteArray()
byte[] com.google.common.io.Files.toByteArray(File file)

//Using ByteStreams.toByteArray
byte[] com.google.common.io.ByteStreams.toByteArray(InputStream is)

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java Read File to Byte Array”

  1. 1 )Write a code which will construct a line for me two points ( .————————.) ?
    Output should be “these line is made my these two points”

    2) I have one Bottle object.I want to design all the oops concepts using that object can anyone explain me ?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.