如何在 Java 中将 InputStream 对象转换为字符串?
java 8object oriented programmingprogramming更新于 2025/9/19 21:00:17
Java 提供 I/O 流来读取和写入数据,其中 Stream 表示输入源或输出目标,可以是文件、I/O 设备、其他程序等。
有两种类型的流可用 −
- InputStream − 用于从源读取(顺序)数据。
- OutputStream − 用于将数据写入目标。
FileInputStream
此类从特定文件读取数据(逐字节)。它通常用于读取带有原始字节的文件内容,例如图像。
将 InputStream 对象转换为 String
您可以使用核心 Java 通过多种方式将 InputStream Object int 转换为 String。您还可以为此使用外部库,如 IOUtils、Guava。以下是在 Java 中将 InputStream 对象转换为 String 的一些方法(不包括外部库)。
使用 BufferedReader
BufferedReader 类的 readLine() 方法从当前读取器的内容中读取一行。使用此方法将 InputStream 对象 int 转换为字符串。
- 通过将 InputStream 对象作为参数传递,实例化 InputStreamReader 类。
- 然后,通过将上面获得的 InputStreamReader 对象作为参数传递,创建一个 BufferedReader。
- 现在,使用 readLine() 方法从此读取器读取每一行并将其附加到 StringBuffer 对象。
- 最后使用 toString() 方法将 StringBuffer 转换为 String。
示例
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class InputStreamToString{
public static void main(String args[]) throws IOException {
//创建一个 InputStream 对象
InputStream inputStream = new FileInputStream("D:/sample.txt");
//创建一个 InputStreamReader 对象
InputStreamReader isReader = new InputStreamReader(inputStream);
//创建一个 BufferedReader 对象
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String str;
while((str = reader.readLine())!= null){
sb.append(str);
}
System.out.println(sb.toString());
}
}
输出
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
使用 Scanner 类
Scanner 类的 nextLine() 方法逐行读取底层 inputStream 的内容。使用此方法将 InputStream 对象 int 转换为 String。
- 通过将 InputStream 对象作为参数传递来实例化 Scanner 类。
- 使用 nextLine() 方法从此 Scanner 读取每一行并将其附加到 StringBuffer 对象。
- 最后使用 toString() 方法将 StringBuffer 转换为 String。
示例
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class InputStreamToString {
public static void main(String args[]) throws IOException {
//创建 InputStream 对象
InputStream inputStream = new FileInputStream("D:/sample.txt");
//创建 Scanner 对象
Scanner sc = new Scanner(inputStream);
//将数据从 scanner 逐行读取到 StringBuffer
StringBuffer sb = new StringBuffer();
while(sc.hasNext()){
sb.append(sc.nextLine());
}
System.out.println(sb.toString());
}
}
输出
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
使用 InputStreamReader 类
InputStreamReader 类的 read() 方法接受字符数组作为参数,并将当前 Stream 的内容读取到给定数组中。使用此方法将 InputStream Object int 转换为 String。
- 通过将 InputStream 对象作为参数传递,实例化 InputStreamReader 类。
- 使用 InputStreamReader 类的 read() 方法将当前流读取器的内容读取到字符数组中。
- 最后通过将字符作为参数传递给其构造函数,将其转换为字符串。
示例
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileInputStreamExample {
public static void main(String args[]) throws IOException {
//创建一个 File 对象
File file = new File("D:/sample.txt");
//创建一个 InputStream 对象
InputStream inputStream = new FileInputStream(file);
//创建一个 InputStreamReader 对象
InputStreamReader isReader = new InputStreamReader(inputStream);
//创建一个字符数组
char charArray[] = new char[(int) file.length()];
//读取阅读器的内容
isReader.read(charArray);
//将字符数组转换为字符串
String contents = new String(charArray);
System.out.println(contents);
}
}
输出
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

