以下是三种将整个文件读入内存以进行处理的方法:
方法A:
方法A:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
方案B:
ByteArrayInputStream bi =
new ByteArrayInputStream(
org.apache.commons.io.FileUtils.readFileToByteArray(file))
方案C:
File file = new File(yourFileName);
RandomAccessFile ra = new RandomAccessFile(yourFileName, "rw"):
byte[] b = new byte[(int)file.length()];
try {
ra.read(b);
} catch (Exception e) {
e.printStackTrace();
}
我为什么会更喜欢某种方法而不喜欢另一种方法?
有没有特定的用例需要选择一种方法而不是另一种方法?
为什么不使用固定长度的 byte[]
代替?