Convert byte[] array to File using Java Last Updated : 18 May, 2022 Comments Improve Suggest changes 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 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 Program to convert Byte Array to Writer in Java References: Writer Class Approach: Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used. Below is the implementation of the above appr 2 min read Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give 5 min read Convert Vector to ArrayList in Java There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right 3 min read Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character 3 min read How to Convert Character Array to String in Java? Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string 4 min read How to Write JSON Array to CSV File using Java? We will see how to read a JSONArray from a JSON file and write the contents to a CSV file using Java. JavaScript Object Notation (JSON) is a standard text-based format for representing structured data that is based on JavaScript object syntax. It is commonly used for transmitting data in web applica 3 min read Conversion of Array To ArrayList in Java Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing 5 min read How to Convert Vector to Array in Java? As we all know an array is a group of liked-typed variables that are referred to by a common name while on the other hand vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implement the List interface which gives a superior 3 min read Java Program to convert Character Array to IntStream Given a Character Array, the task is to convert this array into an IntStream containing the ASCII values of the character elements. Examples: Input: char[] = { 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115 Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' } 1 min read Like