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 classesimport java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;Â
// Main classpublic 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 classesimport java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;Â
// Main classpublic 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Â
