With Java:
I have a byte[]
that represents a file.
How do I write this to a file (ie. C:\myfile.pdf
)
I know it's done with InputStream, but I can't seem to work it out.
With Java:
I have a byte[]
that represents a file.
How do I write this to a file (ie. C:\myfile.pdf
)
I know it's done with InputStream, but I can't seem to work it out.
Use Apache Commons IO
Or, if you insist on making work for yourself...
Basic example:
Also since Java 7, one line with java.nio.file.Files:
Where data is your byte[] and filePath is a String. You can also add multiple file open options with the StandardOpenOptions class. Add throws or surround with try/catch.
From Java 7 onward you can use the try-with-resources statement to avoid leaking resources and make your code easier to read. More on that here.
To write your
byteArray
to a file you would do:Another solution using
java.nio.file
: