How do I read a file into byte array using Commons IO?
Category: commons.io, viewed: 4K time(s).
The following example shows you how to read file contents into byte array. We use the IOUtils class of the commons-io library.
package org.kodejava.example.commons.io;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileToByteArray {
public static void main(String[] args) {
File file = new File("src/main/resources/Hello.txt");
try {
InputStream is = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("Byte array size: " + bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Powered by
More examples on commons.io