如何在Java中将InputStream对象转换为String?
Java提供I/O流来读写数据,其中流代表输入源或输出目标,可以是文件、I/O设备或其他程序等。
有两种类型的流可用:
- InputStream - 用于从源读取(顺序)数据。
- OutputStream - 用于向目标写入数据。
FileInputStream
此类从特定文件(逐字节)读取数据。它通常用于读取包含原始字节的文件内容,例如图像。
将InputStream对象转换为String
您可以使用核心Java通过几种方式将InputStream对象转换为String。您也可以为此目的使用诸如IOUtils、Guava之类的外部库。以下是几种在Java中将InputStream对象转换为String的方法(不包括外部库)。
使用BufferedReader
BufferedReader类的readLine()方法从当前读取器的内容中读取一行。要使用此方法将InputStream对象转换为String。
- 通过将您的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 { //Creating an InputStream object InputStream inputStream = new FileInputStream("D:/sample.txt"); //creating an InputStreamReader object InputStreamReader isReader = new InputStreamReader(inputStream); //Creating a BufferedReader object 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对象转换为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 { //Creating an InputStream object InputStream inputStream = new FileInputStream("D:/sample.txt"); //Creating a Scanner object Scanner sc = new Scanner(inputStream); //Reading line by line from scanner to 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()方法接受字符数组作为参数,并将当前流的内容读取到给定的数组中。要使用此方法将InputStream对象转换为String。
- 通过将您的InputStream对象作为参数来实例化InputStreamReader类。
- 使用InputStreamReader类的read()方法将当前流读取器的内容读取到字符数组中。
- 最后,通过将其作为参数传递给其构造函数来将字符转换为String。
示例
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 { //Creating a File object File file = new File("D:/sample.txt"); //Creating an InputStream object InputStream inputStream = new FileInputStream(file); //creating an InputStreamReader object InputStreamReader isReader = new InputStreamReader(inputStream); //Creating a character array char charArray[] = new char[(int) file.length()]; //Reading the contents of the reader isReader.read(charArray); //Converting character array to a String 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.
广告