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

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

In the last article, we introduced CharArrayReader, which explained its basic concepts and some commonly used methods. Because it is easier to understand, no specific examples are given. In this article, we will continue to introduce another implementation class of Reader: InputStreamReader.

conceptual understanding

For the understanding of InputStreamReader, let's first look at a picture sent before:

image.png

As can be seen from the figure, InputStreamReader is also inherited from the Reader class, and InputStreamReader and FileReader / File also have a certain relationship.

The role of InputStreamReader itself is to convert a byte stream to a character stream.

InputStreamReader acts as a bridge from byte stream to character stream, it converts byte stream to character stream output, and it can specify character set for byte stream.

After specifying the character set, you can output characters one by one; if the encoding of the character set is not specified, the platform's default character encoding will be used during the decoding process.

InputStreamReader reads bytes using the specified charset while decoding them into characters.

The character set it uses can be specified (by name or explicitly), if not, the platform default character set will be used.

When the conversion stream takes effect, it needs to cooperate with other input streams to convert the data in other input streams into data in character units.

After each call to the InputStreamReader's read() method, it causes one or more bytes to be read from the input stream passed as a construction parameter.

If efficient byte-to-character conversion is required in development, we can read more bytes from the incoming stream ahead of time than are needed to satisfy the current read operation, thus avoiding some possible A problem caused by inconsistent character byte placement.

Constructor

// 使用默认字符集创建一个InputStreamReader。
InputStreamReader(InputStream in)
// 使用给定字符集创建一个InputStreamReader 实例。
InputStreamReader(InputStream in, Charset cs)
// 使用给定字符集解码器创建InputStreamReader 实例。
InputStreamReader(InputStream in, CharsetDecoder dec)
// 同上,只不过字符集参数不一致。
InputStreamReader(InputStream in, String charsetName)
复制代码

Common method

// 关闭流并释放资源。
void close()   
// 返回字符编码名称
String getEncoding()
// 读取单个字符
int read()
// 根据参数,将字符读入,并置入数组中的某一部分
int read(char[] cbuf, int offset, int length)
复制代码

Example of use

public static void transReadNoBuf() throws IOException {
  // 读取文件的数据
  InputStream inputStream = new FileInputStream("copy.txt");  
  InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//读取  

  char[] charArray = new char[10];
  int len = inputStreamReader.read(charArray);
  System.out.println(new String(charArray,0,len));
  inputStreamReader.close();
}
复制代码

Looking at the example is also relatively clear, it is FileINputStream itself, and then converts it into a character stream for output through InputStreamReader.

Summarize

This article is about the concept and usage of InputStreamReader, and it is also the introduction of the last common class in the Reader implementation class. Subsequent articles will introduce the relevant content of the character output stream Writer.

Guess you like

Origin juejin.im/post/7086448237657194510