java.io
包中的InputStreamReader
类可用于将字节数据转换为字符数据。
它继承了抽象类Reader
。
InputStreamReader
类与其他输入流一起工作。它也被称为字节流和字符流之间的桥梁。这是因为InputStreamReader
将输入流中的字节读取为字符。
例如,某些字符需要2个字节才能存储在存储器中。为了读取此类数据,我们可以使用输入流读取器,它将2个字节一起读取并转换为相应的字符。
创建 InputStreamReader
为了创建InputStreamReader
,我们必须首先导入java.io.InputStreamReader
包。一旦我们导入了包,创建输入流读取器的方法如下。
// Creates an InputStream
FileInputStream file = new FileInputStream(String path);
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
在上面的示例中,我们创建了一个名为input的InputStreamReader
,以及一个名为file的FileInputStream
。
在这里,文件中的数据使用某种默认字符编码存储。
但是,我们也可以在文件中指定字符编码的类型(UTF8或UTF16)。
// Creates an InputStreamReader specifying the character encoding
InputStreamReader input = new InputStreamReader(file, Charset cs);
在这里,我们使用了Charset
类来指定文件中的字符编码。
InputStreamReader的方法
InputStreamReader
类提供了Reader
类中不同方法的实现。
read() 方法
read()
- 从读取器读取单个字符read(char[] array)
- 从读取器读取字符并存储在指定的数组中read(char[] array, int start, int length)
- 从读取器读取等于length的字符数,并从start开始存储在指定的数组中
例如,假设我们有一个名为input.txt的文件,内容如下。
This is a line of text inside the file.
让我们尝试使用InputStreamReader
读取此文件。
import java.io.InputStreamReader;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
// Creates an array of character
char[] array = new char[100];
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
// Reads characters from the file
input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);
// Closes the reader
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
输出
Data in the stream: This is a line of text inside the file.
在上面的示例中,我们使用文件输入流创建了一个输入流读取器。输入流读取器与input.txt文件链接。
FileInputStream file = new FileInputStream("input.txt");
InputStreamReader input = new InputStreamReader(file);
为了从文件中读取字符,我们使用了read()
方法。
getEncoding() 方法
getEncoding()
方法可用于获取用于在输入流中存储数据的编码类型。例如,
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader with default encoding
InputStreamReader input1 = new InputStreamReader(file);
// Creates an InputStreamReader specifying the encoding
InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));
// Returns the character encoding of the input stream
System.out.println("Character encoding of input1: " + input1.getEncoding());
System.out.println("Character encoding of input2: " + input2.getEncoding());
// Closes the reader
input1.close();
input2.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
输出
The character encoding of input1: Cp1252 The character encoding of input2: UTF8
在上面的示例中,我们创建了两个名为input1和input2的输入流读取器。
- input1未指定字符编码。因此,
getEncoding()
方法返回默认字符编码的规范名称。 - input2指定了字符编码UTF8。因此,
getEncoding()
方法返回指定的字符编码。
注意:我们使用了Charset.forName()
方法来指定字符编码的类型。要了解更多信息,请访问Java Charset (官方Java文档)。
close() 方法
要关闭输入流读取器,我们可以使用close()
方法。调用close()
方法后,我们无法再使用读取器读取数据。
InputStreamReader的其他方法
方法 | 描述 |
---|---|
ready() |
检查流是否已准备好读取 |
mark() |
标记流中已读取数据的为止 |
reset() |
将控制返回到流中设置标记的点 |
要了解更多信息,请访问Java InputStreamReader (官方Java文档)。