Skip to content
Related Articles

Related Articles

Convert byte[] array to File using Java
  • Last Updated : 11 Dec, 2018

To convert byte[] to file getBytes() method of String class is used, and simple write() method can be used to convert that byte into a file.

Program 1: Convert a String into byte[] and write in a file.




// Java Program to convert
// byte array to file
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
  
public class GFG {
  
    // Path of a file
    static String FILEPATH = "";
    static File file = new File(FILEPATH);
  
    // Method which write the bytes into a file
    static void writeByte(byte[] bytes)
    {
        try {
  
            // Initialize a pointer
            // in file using OutputStream
            OutputStream
                os
                = new FileOutputStream(file);
  
            // Starts writing the bytes in it
            os.write(bytes);
            System.out.println("Successfully"
                               + " byte inserted");
  
            // Close the file
            os.close();
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        String string = "GeeksForGeeks"
                        + " - A Computer Science"
                        + " Portal for geeks";
  
        // Get byte array from string
        byte[] bytes = string.getBytes();
  
        // Convert byte array to file
        writeByte(bytes);
    }
}


Output:

Successfully byte inserted

Program 2: To Write Integer, Double, Character Value in the File (using Wrapper Class).




// Java Program to convert
// int, char and double into bytes
// and write it in a file
  
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
  
public class GFG {
  
    // Path of a file
    static String FILEPATH = "";
    static File file = new File(FILEPATH);
  
    // Method which write the bytes into a file
    static void writeByte(byte[] byteInt,
                          byte[] byteChar, byte[] byteDouble)
    {
  
        try {
  
            // Initialize a pointer in file using OutputStream
            OutputStream os = new FileOutputStream(file);
  
            // Starts writing the bytes in it
  
            // Write int value
            os.write(byteInt);
  
            // Write char value
            os.write(byteChar);
  
            // Write double value
            os.write(byteDouble);
  
            System.out.println("Successfully byte inserted");
  
            // Close the file
            os.close();
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int num = 56;
        char ch = 's';
        double dec = 78.9;
  
        // Insert int value
        byte[] byteInt = Integer.toString(num).getBytes();
  
        // Insert char value
        byte[] byteChar = Character.toString(ch).getBytes();
  
        // Insert double value
        byte[] byteDouble = Double.toString(dec).getBytes();
  
        writeByte(byteInt, byteChar, byteDouble);
    }
}


Output:

Successfully byte inserted

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :