Introduction and summary of Java input and output related concepts (File, IO stream) (10)

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

In the previous article, we mainly explained the concept and usage of classes and Filetwo abstract classes of byte streams InputStream / OutputStreamand their subclasses. The previous article Introduction and summary of Java input and output related concepts (File, IO stream) (9) is the last article about InputStream and OutputStream. From now on, we will start to introduce the content related to character stream.

In Java IO, character streams have two related abstract classes Reader / Writer. Let's start with character input streams Reader.

Basic Concepts of Reader

The Reader class itself is an abstract class and is also used to process input streams. The difference between it and InputStream is: it is the common parent class of the input stream in units of characters (referred to as character input stream).

Inheritance of the Reader class

Each subclass corresponding to Reader is shown in the figure:

Screen Shot 2022-04-09 at 2.14.32 PM.png

Some of the commonly used implementation classes are:

  • BufferedReader: This is very similar to the concept of BufferedInputStream, that is, after reading the text from the stream, and then writing it to the cache, it can improve efficiency. For the size of the cache, the user can specify it manually, and the default size is also set in the code. In our daily use, we can directly use the default size. Characters, arrays and lines can be read.

  • InputStreamReader: As mentioned in the previous introduction, the function is to convert bytes into characters, which can solve the problem of garbled characters often encountered.

  • FileReader: Provided for reading character files.

Some functions of the Reader class

The constructor in the Reader abstract class is as follows:

protected Reader() //创建一个新的字符流 reader,其重要部分将同步其自身的 reader。

// 其英文注释为:
// Creates a new character-stream reader whose critical sections will
// synchronize on the given object.
// 意思就是:创建一个新的字符流reader,其重要部分将同步给定的对象。
// 因为这是个抽象类,所以具体的解释我们放到后续的子类介绍中进行讲解。
// 对于这两种构造方法,不带参数时,将自己作为锁;如果传入了某个Object 对象,则将这个对象其作为锁。
protected Reader(Object lock)
复制代码

method:

// 关闭流,释放资源
abstract  void  close()

// 标记流中的此时当前的位置
void  mark(int readAheadLimit)

// 是否支持标记操作(上面的mark 函数的作用)
boolean  markSupported()

// 读取一个字符
int  read()

// 将字符读入cbuf 数组
int  read(char[] cbuf)

// 将字符读入数字,存入参数中的位置
abstract  int  read(char[] cbuf, int off, int len)

// 将字符读入此字符缓冲区
int  read(CharBuffer target)

// 判断是否准备好读取这个流
boolean  ready()

// 重置流
void  reset()

// 跳过字符
long  skip(long n)
复制代码

Summarize

The above only describes the relevant structure of Reader, but because Reader itself is an abstract class, the specific use will be discussed later when each inherited subclass is introduced.

Subsequent articles will introduce the subclasses of Reader in detail.

Guess you like

Origin juejin.im/post/7084992608061095967