Convert byte[] array to File using Java Last Updated : 18 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report As we know whenever it comes to writing over a file, write() method of the File class comes into play but here we can not use it in order to convert that byte into a file. In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file. Example: Java // Java Program to convert Byte Array to File // Importing required classes import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; // Main class public class GFG { // Path of a file static String FILEPATH = ""; static File file = new File(FILEPATH); // Method 1 // To write the bytes into a file static void writeByte(byte[] bytes) { // Try block to check for exceptions try { // Initialize a pointer in file // using OutputStream OutputStream os = new FileOutputStream(file); // Starting writing the bytes in it os.write(bytes); // Display message onconsole for successful // execution System.out.println("Successfully" + " byte inserted"); // Close the file connections os.close(); } // Catch block to handle the exceptions catch (Exception e) { // Display exception on console System.out.println("Exception: " + e); } } // Method 2 // Main driver method public static void main(String args[]) { // Input string String string = "GeeksForGeeks" + " - A Computer Science" + " Portal for geeks"; // Getting byte array from string // using getBytes() method byte[] bytes = string.getBytes(); // Calling Method 1 to // convert byte array to file writeByte(bytes); } } Output: On console Successfully byte inserted Now let us also discuss the use-case in order how to write Integer, Double, Character Values in the File (using Wrapper Class) Example: Java // Java Program to Convert Integer, Character and Double // Types into Bytes and Writing it in a File // Importing required classes import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; // Main class public class GFG { // Path of a file static String FILEPATH = ""; // Getting the file via creating File class object static File file = new File(FILEPATH); // Method 1 // Writing the bytes into a file static void writeByte(byte[] byteInt, byte[] byteChar, byte[] byteDouble) { // Try block to check for exceptions try { // Initialize a pointer in file // using OutputStream class object OutputStream os = new FileOutputStream(file); // Starting writing the bytes in it // Writing int value os.write(byteInt); // Writing char value os.write(byteChar); // Writing double value os.write(byteDouble); // Display message for successful execution of // program System.out.println( "Successfully byte inserted"); // Close the file connections // using close() method os.close(); } // Catch block to handle exceptions catch (Exception e) { // Display message when exceptions occurs System.out.println("Exception: " + e); } } // Method 2 // Main driver method public static void main(String args[]) { // Declaring and initializing data types int num = 56; char ch = 's'; double dec = 78.9; // Inserting integer value byte[] byteInt = Integer.toString(num).getBytes(); // Inserting character value byte[] byteChar = Character.toString(ch).getBytes(); // Inserting double value byte[] byteDouble = Double.toString(dec).getBytes(); // Calling Method 1 to // write the bytes into a file writeByte(byteInt, byteChar, byteDouble); } } Output: On console Successfully byte inserted Comment More infoAdvertise with us Next Article Convert byte[] array to File using Java B bilal-hungund Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2018 Java-Arrays Java-Byte Java-Array-Programs +2 More Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like