How to Read and Write Binary Files in Java? Last Updated : 06 May, 2025 Comments Improve Suggest changes Like Article Like Report The Binary files contain data in a format that is not human-readable. To make them suitable for storing complex data structures efficiently, in Java, we can read from and write to binary files using the Input and Output Streams.In this article, we will learn and see the code implementation to read and write binary files in Java.Syntax to Read from a Binary File:InputStream inputStream = new FileInputStream("data.bin");int data;while ((data = inputStream.read()) != -1) { // Process the read data}inputStream.close();Syntax to Write to a Binary File:OutputStream outputStream = new FileOutputStream("data.bin");// Write data to the output streamoutputStream.close();Program to Read and Write Binary Files in JavaBelow is the implementation of Read and Write Binary Files in Java: Java // Java Program to Read and Write Binary Files import java.io.*; // Drver Class public class GFG { // Main Function public static void main(String[] args) { try { // Writing to binary file OutputStream Stream = new FileOutputStream("data.bin"); Stream.write(new byte[]{0x48, 0x65, 0x6C, 0x6C, 0x6F}); // ASCII values for "Hello" Stream.close(); // Reading from a binary file InputStream inputStream = new FileInputStream("data.bin"); int data; while ((data = inputStream.read()) != -1) { System.out.print((char) data); // Convert byte to character } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } OutputHello Explanation of the above Program:Writing to a Binary File:We create an OutputStream object using the FileOutputStream and specify the file name.We use the write() method to write binary data to output stream.Reading from a Binary File:We create an InputStream object using the FileInputStream and specify the file name.We use the read() method to read binary data from input stream storing each byte in an integer variable.We convert each byte to its corresponding ASCII character using the typecasting and print it to console. Comment More infoAdvertise with us Next Article How to Read and Write Binary Files in Java? V vikas2gqb5 Follow Improve Article Tags : Java Java Programs Java-Files Java Examples Practice Tags : Java Similar Reads How to Read and Write Files Using the New I/O (NIO.2) API in Java? In this article, we will learn how to read and write files using the new I/O (NIO) API in Java. For this first, we need to import the file from the NIO package in Java. This NIO.2 is introduced from the Java 7 version. This provides a more efficient way of handling input and output operations compar 3 min read Java Program to Write into a File FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:Using writeString( 5 min read How to Zip and Unzip Files in Java? In Java Programming we have a lot of features available for solving real-time problems. Here I will explain how to Zip files and How to Unzip files by using Java Programming. Mostly zip and unzip files are used for zip the files for save storage, easy to share, and other purposes. In this article, w 3 min read How to Read a File From the Classpath in Java? Reading a file from the classpath concept in Java is important for Java Developers to understand. It is important because it plays a crucial role in the context of Resource Loading within applications. In Java, we can read a file from the classpath by using the Input Stream class. By loading the cla 3 min read How to Create a File with a Specific Owner and Group in Java? Java is a basic tool that does not directly let you choose who owns a file or which group it belongs to when you create it. You have to rely on other methods or external libraries if you need to control these aspects. In this article, we will learn How to create a file with a specific owner and grou 3 min read Like