FileReader也有缓冲方法

huangapple go评论59阅读模式
英文:

FileReader has also buffered method

问题

以下是您要翻译的内容:

我刚刚发现了这个

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

那么,既然FileReader本身有一个可以读取多于1个字节的方法,为什么我要使用BufferedReader呢?

英文:

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.

答案1

得分: 1

因为您传递给read(char[])方法的字符数组长度只有100个字符,远远小于从硬盘读取的最有效块大小。缓冲区允许您以对您的代码方便的大小进行读取,同时仍然保持硬件层的最佳读取大小。

那么,对于BufferedReader来说,最佳的读取大小是多少呢?这取决于底层数据源。如果您不知道它,可以使用默认大小。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月2日 14:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64167244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定