Here, we will go through the different ways to convert file to byte array in Java.
Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs.
Methods:
- Using read(byte[]) method of FileInputStream class
- Using Files.readAllBytes() method
FileInputStream is useful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. read(byte[]) method of FileInputStream class which reads up to the length of the file and then converts bytes of data from this input stream into the byte array.
Procedure:
- Create an instance of File Input Stream with the file path.
- Create a byte array of the same length as the file.
- Read that file content to an array.
- Print the byte array.
- Close the instance of the file input stream as it is a good practice in order to avoid any exception or error being faced during runtime and to release the memory resources making our program optimized leading to faster execution.
Implementation: In order to illustrate the conversion of a text file present in the local directory on a machine to the byte array, we will be considering a random file named say it be ‘demo.rtf’ which is present in the local directory.

Example:
Java
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class GFG {
public static byte [] method(File file)
throws IOException
{
FileInputStream fl = new FileInputStream(file);
byte [] arr = new byte [( int )file.length()];
fl.read(arr);
fl.close();
return arr;
}
public static void main(String[] args)
throws IOException
{
File path = new File(
"/Users/mayanksolanki/Desktop/demo.rtf" );
byte [] array = method(path);
System.out.print(Arrays.toString(array));
}
}
|
Output:

Method 2: Using readAllBytes() method of Files class
java.nio.file.Files class has pre-defined readAllBytes() method which reads all the bytes from a file.
Procedure:
- Take a text file path
- Convert that file into a byte array by calling Files.readAllBytes().
- Print the byte array.
Example:
Java
import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
throws IOException
{
Path path = Paths.get(
"/Users/mayanksolanki/Desktop/demo.rtf" );
byte [] arr = Files.readAllBytes(path);
System.out.println(Arrays.toString(arr));
}
}
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Apr, 2022
Like Article
Save Article