IUn*_*own 5 java file-io inputstream
在处理之前,有三种方法可以将整个文件读入内存:
方法A:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
Run Code Online (Sandbox Code Playgroud)
方法B:
ByteArrayInputStream bi =
new ByteArrayInputStream(
org.apache.commons.io.FileUtils.readFileToByteArray(file))
Run Code Online (Sandbox Code Playgroud)
方法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();
}
Run Code Online (Sandbox Code Playgroud)
为什么我更喜欢一种方法呢?
是否有任何特定的用例需要一种方法而不是另一种方法?
为什么不使用固定长度byte[]呢?
| 归档时间: |
|
| 查看次数: |
9588 次 |
| 最近记录: |