InputStreamReader
时间: 2023-10-25 13:24:27 浏览: 26
The InputStreamReader class is a subclass of the Reader class in Java. It reads bytes from an InputStream and decodes them into characters using a specified character encoding. The decoded characters are then made available to the programmer as a stream of characters.
The InputStreamReader class is used when we want to read data from an InputStream and process it as text. It provides a convenient way to convert binary data into text data.
Here is an example of using an InputStreamReader to read data from an InputStream:
```java
InputStream inputStream = new FileInputStream("data.txt");
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
int data = reader.read();
while (data != -1) {
char ch = (char) data;
System.out.print(ch);
data = reader.read();
}
reader.close();
inputStream.close();
```
In this example, we create an InputStream from a file called "data.txt". We then create an InputStreamReader using the UTF-8 character encoding. We read data from the InputStreamReader one character at a time using the read() method. The read() method returns -1 when the end of the stream is reached. Finally, we close the InputStreamReader and the InputStream.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)