Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a temporary java.io.File from byte[]

Tags:

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.

like image 809
Adrien Gorrell Avatar asked Sep 25 '13 13:09

Adrien Gorrell


People also ask

Can we convert byte array to file 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.

How do you create a byte file in Java?

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

How do I create a byte file?

write(bytes, new File(path)); With Apache Commons: FileUtils. writeByteArrayToFile(new File(path), bytes);


1 Answers

File tempFile = File.createTempFile(prefix, suffix, null); FileOutputStream fos = new FileOutputStream(tempFile); fos.write(byteArray); 

Check out related docs:

File.createTempFile(prefix, suffix, directory);

like image 101
TheKojuEffect Avatar answered Sep 19 '22 06:09

TheKojuEffect