Saturday, 17 August 2013

BufferInputStream vs ByteArrayInputStream

BufferInputStream vs ByteArrayInputStream

Here are three ways to read an entire file into memory before processing it:
Approach A:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
Approach B:
ByteArrayInputStream bi =
new ByteArrayInputStream(
org.apache.commons.io.FileUtils.readFileToByteArray(file))
Approach 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();
}
Why would I prefer one approach over another?
Are there any specific use-cases that call for one approach over another?
Why not use a fixed-length byte[] instead?

No comments:

Post a Comment