Convert bytes[] to 'File' in Java
66,483
Solution 1
try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
fileOuputStream.write(byteArray);
}
Solution 2
Try this:
Files.write(Paths.get("filename"), bytes);
Solution 3
A simple program that will write a byte[] into a file.
import java.io.File;
import java.io.FileOutputStream;
public class BytesToFile {
public static void main(String[] args) {
byte[] demBytes = null; // Instead of null, specify your bytes here.
File outputFile = new File("LOCATION TO FILE");
try ( FileOutputStream outputStream = new FileOutputStream(outputFile); ) {
outputStream.write(demBytes); // Write the bytes and you're done.
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: The try-catch statement requires a Java version >= 1.7. Remember to change the bytes from null to correspond to your byte array.
Author by
Bob
Updated on September 01, 2021Comments
-
Bob almost 3 years
I have an array of bytes
bytes[] doc
How can I create an instance of
new File
from those bytes?