Convert byte[] array to File using Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Successfully byte inserted

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Successfully byte inserted



My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.