1. Introduction
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:
package com.frontbackend.java.io.conversions.frombytearray.tofile;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ByteArrayToFileUsingFileOutputStream {
public static void main(String[] args) throws IOException {
byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);
try (FileOutputStream fos = new FileOutputStream("/tmp/output.txt")) {
fos.write(bytes);
}
}
}
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 JDK7s
Files` class:
package com.frontbackend.java.io.conversions.frombytearray.tofile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class ByteArrayToFileUsingFiles {
public static void main(String[] args) throws IOException {
byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);
InputStream inputStream = new ByteArrayInputStream(bytes);
File file = new File("/tmp/output.txt");
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
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.
package com.frontbackend.java.io.conversions.frombytearray.tofile;
import static com.google.common.io.Files.write;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ByteArrayToFileUsingGuava {
public static void main(String[] args) throws IOException {
byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);
File file = new File("/tmp/output.txt");
write(bytes, 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
package com.frontbackend.java.io.conversions.frombytearray.tofile;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
public class ByteArrayToFileUsingFileUtils {
public static void main(String[] args) throws IOException {
byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);
FileUtils.writeByteArrayToFile(new File("/tmp/output.txt"), bytes);
}
}
7. Conclusion
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.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}