I have an array of bytes
bytes[] doc
How can I create an instance of new File
from those bytes?
I have an array of bytes
bytes[] doc
How can I create an instance of new File
from those bytes?
try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
fileOuputStream.write(byteArray);
}
Try this:
Files.write(Paths.get("filename"), bytes);
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.