Apache Commons
How do I read a file into byte array using Apache Commons IO?
The following example shows you how to read file contents into byte array. We use the IOUtils class of the Apache Commons IO library. package org.kodejava.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("README.md"); try { InputStream is […]
Artikel oleh Wayan · Sumber: Kode Java ↗
Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.
The following example shows you how to read file contents into byte array. We use the IOUtils class of the Apache Commons IO library.
package org.kodejava.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("README.md");
try {
InputStream is = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
System.out.println("Byte array size: " + bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
