Skip to content
Related Articles

Related Articles

Convert byte[] array to File using Java

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 18 May, 2022

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 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!