如何在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.
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP