跳到主要内容

Java InputStreamReader 类

提示
  1. InputStreamReader 类概述:Java InputStreamReader 类属于 java.io 包,用于将字节流转换为字符流,它扩展了 Reader 类。
  2. 创建和使用 InputStreamReader:通过 InputStreamReader 可以将字节数据(如从 FileInputStream)转换为字符数据,支持指定字符编码。
  3. InputStreamReader 的主要方法:提供方法如 read() 用于读取字符、getEncoding() 获取字符编码类型、以及 close() 关闭阅读器。其它方法包括 ready(), mark(), reset() 用于检查、标记、重置流。

java.io 包中的 InputStreamReader 类可用于将字节数据转换为字符数据。

它扩展了抽象类 Reader

InputStreamReader 类是 Java Reader 的子类。

InputStreamReader 类可以与其他输入流一起工作。它也被称为字节流和字符流之间的桥梁。这是因为 InputStreamReader 将输入流中的字节作为字符读取。

例如,有些字符在存储时需要 2 个字节。为了读取这样的数据,我们可以使用输入流阅读器,它一次读取 2 个字节并将其转换为相应的字符。

创建一个 InputStreamReader

为了创建一个 InputStreamReader,我们首先必须导入 java.io.InputStreamReader 包。一旦我们导入了包,以下是我们可以创建输入流阅读器的方式。

// 创建一个 InputStream
FileInputStream file = new FileInputStream(String path);

// 创建一个 InputStreamReader
InputStreamReader input = new InputStreamReader(file);

在上面的例子中,我们创建了一个名为 inputInputStreamReader,以及一个名为 fileFileInputStream

这里,文件中的数据使用某种默认字符编码存储。

然而,我们也可以在文件中指定字符编码的类型(UTF8UTF16)。

// 创建一个指定字符编码的 InputStreamReader
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 的文件,内容如下。

这是文件中的一行文字。

让我们尝试使用 InputStreamReader 读取这个文件。

import java.io.InputStreamReader;
import java.io.FileInputStream;

class Main {
public static void main(String[] args) {

// 创建一个字符数组
char[] array = new char[100];

try {
// 创建一个 FileInputStream
FileInputStream file = new FileInputStream("input.txt");

// 创建一个 InputStreamReader
InputStreamReader input = new InputStreamReader(file);

// 从文件中读取字符
input.read(array);
System.out.println("流中的数据:");
System.out.println(array);

// 关闭阅读器
input.close();
}

catch(Exception e) {
e.getStackTrace();
}
}
}

输出

流中的数据:
这是文件中的一行文字。

在上面的示例中,我们使用文件输入流创建了一个输入流阅读器。输入流阅读器与文件 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 {
// 创建一个 FileInputStream
FileInputStream file = new FileInputStream("input.txt");

// 创建一个具有默认编码的 InputStreamReader
InputStreamReader input1 = new InputStreamReader(file);

// 创建一个指定编码的 InputStreamReader
InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));

// 返回输入流的字符编码
System.out.println("input1 的字符编码: " + input1.getEncoding());
System.out.println("input2 的字符编码: " + input2.getEncoding());

// 关闭阅读器
input1.close();
input2.close();
}

catch(Exception e) {
e.getStackTrace();
}
}
}

输出

input1 的字符编码: Cp1252
input2 的字符编码: UTF8

在上面的示例中,我们创建了两个名为 input1input2 的输入流阅读器。

  • input1 没有指定字符编码。因此,getEncoding() 方法返回默认字符编码的规范名称。
  • input2 指定了字符编码,UTF8。因此,getEncoding() 方法返回指定的字符编码。

注意:我们使用了 Charset.forName() 方法来指定字符编码的类型。要了解更多,请访问 Java Charset(官方 Java 文档)

close() 方法

要关闭输入流阅读器,我们可以使用 close() 方法。一旦调用了 close() 方法,我们就不能使用阅读器来读取数据。

InputStreamReader 的其他方法

方法描述
ready()检查流是否准备好可以被读取
mark()标记流中已读取数据的位置
reset()将控制返回到流中标记设置的点

要了解更多,请访问 Java InputStreamReader(官方 Java 文档)