Java如何将文件读入字节数组?
package org.nhooo.example.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("Hello.txt");
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;
}
}
}
}
热门推荐
6 万寿祝福语简短
10 锡婚祝福语简短霸气
11 药店祝福语简短10字
12 城市祝福语简短10字
13 Q穷人结婚祝福语简短
14 节日祝福语幽默简短的
15 祝老婆生日祝福语简短
16 婆婆生日祝福语简短的
17 简短的小孩春节祝福语
18 低调祝福语简短10字