Java – How to convert File to byte[]
In Java, we can use Files.readAllBytes(path)
to convert a File
object into a byte[]
.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
String filePath = "/path/to/file";
// file to byte[], Path
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
// file to byte[], File -> Path
File file = new File(filePath);
byte[] bytes = Files.readAllBytes(file.toPath());
P.S The NIO Files
class is available since Java 7.
1. FileInputStream
Before Java 7, we can initiate a new byte[]
with a predefined size (same with the file length), and use FileInputStream
to read the file data into the new byte[]
.
// file to byte[], old and classic way, before Java 7
private static void readFileToBytes(String filePath) throws IOException {
File file = new File(filePath);
byte[] bytes = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
//read file into bytes[]
fis.read(bytes);
} finally {
if (fis != null) {
fis.close();
}
}
}
Or this try-with-resources
version.
private static void readFileToBytes(String filePath) throws IOException {
File file = new File(filePath);
byte[] bytes = new byte[(int) file.length()];
// funny, if can use Java 7, please uses Files.readAllBytes(path)
try(FileInputStream fis = new FileInputStream(file)){
fis.read(bytes);
}
}
2. Apache Commons-IO
If we have Apache Commons IO, try FileUtils
.
import org.apache.commons.io.FileUtils;
//...
File file = new File("/path/file");
byte[] bytes = FileUtils.readFileToByteArray(file);
pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
3. Convert File to byte[] and vice versa.
The below example uses the NIO Files.readAllBytes
to read an image file into a byte[]
, and Files.write
to save the byte[]
into a new image file.
FileToBytes.java
package com.mkyong.io.howto;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileToBytes {
public static void main(String[] args) {
try {
String filePath = "/home/mkyong/test/phone.png";
// file to bytes[]
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
// bytes[] to file
Path path = Paths.get("/home/mkyong/test/phone2.png");
Files.write(path, bytes);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-io
try {
byte[] bFile = Files.readAllBytes(Paths.get(“/home/input_test.png”));
// save byte[] into a file
Path path = Paths.get(“/home/output_test.png”);
// The CREATE option is necessary. without it, the file is created with zero bytes the first time
// a write is attempted. The 2nd write works OK.
// With CREATE added, the write works first time whether the file exists or not.
//This next line replaces all of readBytesFromFile
Files.write(path, bFile,StandardOpenOption.CREATE);
System.out.println(“Done”);
} catch (IOException e) {
e.printStackTrace();
}
}
We tested the code with character and binary data, and it works fine without the
StandardOpenOption.CREATE`. Not sure how to simulate the above problem.
//Java 7 Files class:
byte[] data = Files.readAllBytes(file.getPath());
toPath()
Article is updated with java.nio example, thanks for your suggestion.
So usefull!!
Hello ! Thanks for this, it is really helpful. Anyway, I am not able to make it work with a PDF file. The copied file is completely blank. Do you have a way to make this work ? With other file types is working fine. Thanks !!!
can I create bytes [] of database files, like mydb.db file
hello , can you please help me I am converting bytes array to audio file but it is not working?