In this article we are going to present how to convert Byte Array to File using plain Java solutions (also in version JDK7) and libraries like Guava and Apache Commons IO.
2. Save Byte Array in File using FileOutputStream
Let's start with plain Java solution. To convert byte to File we can use FileOutputStream as presented in the following example:
In this example first we create sample byte array, then we open FileOutputStream and write bytes to it using write(...) method
Note that we are using try-with-resources funcion to automatically close our streams.
4. Convert Byte Array to File using JDK7 Files class
Java 7 comes with many great features and improvements. In the following example code we presented how to convert Byte Array to file using JDK7sFiles` class:
This example is pretty streightforward, and gives us one-line solution. We used Files.copy(...) method that convert early prepared ByteArrayInputStream to the file. If the StandardCopyOption.REPLACE_EXISTING is set, the process will replace existing file.
5. Convert Byte Array to File with Guava library
External libraries like Guava comes with great utitlity methods for IO operations and manipulations. In the following example we used Guava to save byte array into a file.
In this example we make a use of Files.write(...) method available with Guava. The method takes byte array and output file as a parameters.
6. Byte Array to File conversion using Apache Commons IO
In this article, we presented how to convert Byte Array to File in Java. We used plain java solutions and libraries like Guava and Apache Commons IO.
As usual, code examples used in this tutorial are available under our GitHub repository.