0

I just found this

FileReader reader = new FileReader("SomePath");
char[] buf = new char[100];
reader.read(buf);

So why should I use BufferedReader if FileReader itself has a method to read more than 1 byte.

Alicia24
  • 21
  • 2
  • 2
    You’re missing the key word: buffered. The more you go to the disk, the more I/O you do. Why do you think caches exist? – Abhijit Sarkar Oct 02 '20 at 06:57

1 Answers1

1

Because the 100 character array you passed to the read(char[]) method is much smaller than the most efficient block size that can be read from the hard drive. The buffer allows you to make reads in sizes that are convenient to your code while still maintaining the optimal read size in the hardware layer.

What's the optimal read size for BufferedReader then? It depends on the underlying data source. If you don't know it, use the default size.

Torben
  • 3,805
  • 26
  • 31
  • static int defaultCharBufferSize = 8192; So default size of BufferedReader is 8192 Bytes. So I can also make FileReader buffer of this size. So what is the difference than. FileReader reader = new FileReader("SomePath"); char[] buf = new char[8192]; reader.read(buf); – Alicia24 Oct 02 '20 at 10:14
  • Explanation for that was already included in the original answer. – Torben Oct 06 '20 at 08:32