Java InputStreamReader 类
Java InputStreamReader 类
在本教程中,我们将通过示例了解 Java InputStreamReader 及其方法。
InputStreamReader
java.io
的类 包可用于将字节数据转换为字符数据。
它扩展了抽象类 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);
在上面的例子中,我们创建了一个 InputStreamReader
命名为 输入 连同 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
在上面的示例中,我们创建了 2 个名为 input1 的输入流读取器 和 input2 .
- 输入1 没有指定字符编码。因此
getEncoding()
方法返回默认字符编码的规范名称。 - 输入2 指定字符编码,UTF8 .因此
getEncoding()
方法返回指定的字符编码。
注意 :我们使用了 Charset.forName()
方法来指定字符编码的类型。要了解更多信息,请访问 Java Charset(官方 Java 文档)。
close() 方法
要关闭输入流阅读器,我们可以使用 close()
方法。一旦 close()
方法被调用,我们不能使用阅读器读取数据。
InputStreamReader的其他方法
方法 | 说明 |
---|---|
ready() | 检查流是否准备好被读取 |
mark() | 标记流中已读取数据的位置 |
reset() | 将控件返回到流中设置标记的点 |
要了解更多信息,请访问 Java InputStreamReader(Java 官方文档)。
java