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 create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023