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 classesimportjava.io.File;importjava.io.FileOutputStream;importjava.io.OutputStream;  // Main classpublicclassGFG {      // Path of a file    staticString FILEPATH = "";    staticFile file = newFile(FILEPATH);      // Method 1    // To write the bytes into a file    staticvoidwriteByte(byte[] bytes)    {          // Try block to check for exceptions        try{              // Initialize a pointer in file            // using OutputStream            OutputStream os = newFileOutputStream(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    publicstaticvoidmain(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 classesimportjava.io.File;importjava.io.FileOutputStream;importjava.io.OutputStream;  // Main classpublicclassGFG {      // Path of a file    staticString FILEPATH = "";      // Getting the file via creating File class object    staticFile file = newFile(FILEPATH);      // Method 1    // Writing the bytes into a file    staticvoidwriteByte(byte[] byteInt, byte[] byteChar,                          byte[] byteDouble)    {          // Try block to check for exceptions        try{              // Initialize a pointer in file            // using OutputStream class object            OutputStream os = newFileOutputStream(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    publicstaticvoidmain(String args[])    {          // Declaring and initializing data types        intnum = 56;        charch = 's';        doubledec = 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Â

 
                                    







