Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a File from byte array

So - I've got a third party library that needs a File as input. I've got a byte array.

I don't want to write the bytes to disk .. I'd like to keep this in memory. Any idea on how I can create a File from the provided byte array (without writing to disk)?

like image 780
desau Avatar asked Sep 13 '10 21:09

desau


People also ask

How do I write a byte array to 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);

How do you create a multipart file from a byte array in java?

Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example: FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile. getName(), 100000000, new java.

Can we get file name from byte array java?

But...if you have a byte array and that is all, it doesn't have a filename - those are only available within the operating system file system for actual files, and a byte array does not have any file associated with it, even if it was read from or has been written to a file.

Which stream is used to write a byte array to a file?

Java ByteArrayOutputStream class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later. The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.


1 Answers

Sorry, not possible. A File is inherently an on-disk entity, unless you have a RAM disk - but that's not something you can create in Java.

That's exactly the reason why APIs should not be based on File objects (or be overloaded to accept an InputStream).

like image 144
Michael Borgwardt Avatar answered Sep 30 '22 13:09

Michael Borgwardt