I must use an existing method that is like saveAttachment(Attachment attachment)
where Attachment
has a File attribute
.
My problem is that I'm retrieving a byte[]
and I want to save it using this method. How can I have a "local" File
just for saving ?
Sorry if my question is dumb, I don't know much about Files in Java.
Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.
Java – How to save byte[] to a file write is the simplest solution to save byte[] to a file. // bytes = byte[] Path path = Paths. get("/path/file"); Files. write(path, bytes);
write(bytes, new File(path)); With Apache Commons: FileUtils. writeByteArrayToFile(new File(path), bytes);
File tempFile = File.createTempFile(prefix, suffix, null); FileOutputStream fos = new FileOutputStream(tempFile); fos.write(byteArray);
Check out related docs:
File.createTempFile(prefix, suffix, directory);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With