I was running the code below to try and understand how BufferedInputStream works in Java. I set the buffer size to 1 and was expecting the buffer to read the file 465 times because that is how much character is in the file. However, it reads the file once. What I found to change the number of times the buffer reads the file, you change the array of bytes, does, size to 1. In this case it reads the file 465 times. I do not understand why buffer reads the file once even though I set the buffer size 1. How come the array "does" dictates how many times the buffer reads the file?
File f = new File("runs");
if(!f.exists()) {
f.createNewFile();
}
FileInputStream input = new FileInputStream(f);
BufferedInputStream b = new BufferedInputStream(input, 1);
byte[] does = new byte[1000];
int i = b.read(does);
int x = 0;
String tmp;
while(i != -1) {
tmp = new String(does, StandardCharsets.UTF_8);
if(!tmp.equalsIgnoreCase("\n")) {
System.out.print(tmp);
}else {
System.out.println(tmp);
}
x++;
i = b.read(does);
}
System.out.println(x);
}