Tony Tony - 3 months ago 7
Java Question

Buffer size in Java

I have got a little question about buffer size in Java. Why do we set buffer size to

1024
or
2^n
. For example:

inputStream = file.getInputStream();
File newFile = new File("C:/uploads/operators.xml");
outputStream = new FileOutputStream(newFile);
int read = 0;

byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();


How
outputStream.write(bytes, 0, read);
works? Why do we use
bytes
array?

Answer

You read bytes length of bytes.length from file stream and stores them in the byte array bytes[]. And then you write bytes in outputStreem form bytes[] array. For more read the Java I/O documentation.

Comments