Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct a java.io.File that reads from a byte array

Tags:

java

I'm using an API that requires a java.io.File as input, but I'd like to just send the API a byte array. Is there a way to create a File object that will read from my byte array rather than from disk?

like image 448
Ben Dilts Avatar asked Sep 01 '25 01:09

Ben Dilts


2 Answers

A File does not read anything. It represents a file (or directory) object in the Operating System (or virtual file, etc.). To read from it, you wil have to create Readers or Streams, which the API will do on its own. If the API does not provide methods to pass in a Reader or InputStream, you are out of luck.

like image 119
Attila Avatar answered Sep 04 '25 14:09

Attila


You could open a temporary file: open temp file in java

Then if take your byte[] and write it all to the temporary file through some OutputStream/Writer you should have what you need. Then you can pass the file without creating it on disk.

like image 24
Sam Stern Avatar answered Sep 04 '25 13:09

Sam Stern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!