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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
- Program to convert Byte Array to Writer in Java
- How to Convert a String value to Byte value in Java with Examples
- How to Convert a Byte value to String value in Java with Examples
- How to Convert a zero terminated byte array to string in Golang?
- Java.Lang.Byte class in Java
- How to get file input by selected file name without path using jQuery ?
- Character Stream Vs Byte Stream in Java
- Byte toString() method in Java with examples
- Byte byteValue() method in Java with examples
- Byte shortValue() method in Java with examples
- Byte floatValue() method in Java with examples
- Byte intValue() method in Java with examples
- Byte doubleValue() method in Java with examples
- Byte longValue() method in Java with examples
- Byte hashCode() method in Java with examples
- Byte compare() method in Java with examples
- Byte compareTo() method in Java with examples
- Byte equals() method in Java with examples
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.

