BufferedReader构造函数对FileReader的期望是什么 [英] What does a BufferedReader constructor expect a FileReader

查看:178
本文介绍了BufferedReader构造函数对FileReader的期望是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要了解这两个类之间的区别以及它们如何相互协作。据我所知,FileReader一次从一个文件中读取字符,BufferedReader读取大量数据并将其存储在缓冲区中,从而使其更快。

I need to understand the difference between these two classes and how they work with each other. I understand that FileReader reads characters from a file one character at a time and the BufferedReader reads a large chunk of data and stores it in a buffer and thus makes it faster.

为了使用BufferedReader,我必须提供一个FileReader。如果BufferedReader类以不同方式读取文件,它如何使用FileReader?这是否意味着BufferedReader使用FileReader,因此在幕后,字符仍然一次只能读取一个字符?我想我的问题是BufferedReader类如何使用FileReader类。

In order to use a BufferedReader, i have to provide it a FileReader. How does the BufferedReader class use the FileReader if it reads the file differently? Does it mean that BufferedReader uses FileReader and therefore behind the scenes the characters are still read one character at a time? I guess my question is how does the BufferedReader class use the FileReader class.

推荐答案

首先, BufferedReader 需要读者,而非 FileReader (虽然接受后者)。

First of all, BufferedReader takes a Reader, not a FileReader (although the latter is accepted).

Reader 抽象类有几个 read()方法。有一个read-one-character版本以及两个版本,它们将一个字符块读入一个数组。

The Reader abstract class has several read() methods. There is a read-one-character version as well as two versions that read a block of characters into an array.

只有使用 BufferedReader 如果您一次只读单个字符或小块。

It only makes sense to use BufferedReader if you're reading single characters or small blocks at a time.

请考虑以下两个请求:

char ch1 = fileReader.read();
char ch2 = bufferedReader.read()

第一个将转到底层文件,第二个很可能会从 BufferedReader 的内部缓冲区中得到满足。

The first one will go to the underlying file, whereas the second one will most probably be satisfied from the BufferedReader's internal buffer.

这篇关于BufferedReader构造函数对FileReader的期望是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆