英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论