package org.kodejava.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadFileIntoByteArray {
public static void main(String[] args) throws IOException {
File file = new File("README.md");
try (InputStream is = new FileInputStream(file)) {
if (file.length() > Integer.MAX_VALUE) {
throw new IOException("File is too large.");
}
int offset = 0;
int bytesRead;
byte[] bytes = new byte[(int) file.length()];
while (offset < bytes.length
&& (bytesRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += bytesRead;
}
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024