Java中InputSteam轉(zhuǎn)String的實(shí)現(xiàn)方法
1、InputStream轉(zhuǎn)化為String
1、使用InputStreamReader和StringBuilder(JDK)
public class InputStream2String {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("E:/duckAndJava/IO/testFile.txt"); //路徑修改為本地文件所在的位置
char[] buffer = new char[1024]; //根據(jù)需要的數(shù)組大小進(jìn)行自定義
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, "UTF-8");
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
String myString = out.toString();
System.out.println("myString = " + myString);
}catch (IOException e){
e.printStackTrace();
}
}
}2、使用inputStream.read()andStringBuilder
StringBuilder sb = new StringBuilder();
for (int ch; (ch = inputStream.read()) != -1; ) {
sb.append((char) ch);
}
String myString = sb.toString();3、使用ByteArrayOutputStreamandinputStream.read
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
String myString = result.toString("UTF-8");ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
String str = result.toString(StandardCharsets.UTF_8.name());
return str;BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
String str = buf.toString();
return str;4、使用BufferedInputStream和ByteArrayOutputStream
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
for (int result = bis.read(); result != -1; result = bis.read()) {
buf.write((byte) result);
}
String myString = buf.toString("UTF-8");5、使用BufferedReader
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
if (result.length() > 0) {
result.append(newLine);
}
result.append(line);
}
String myString = result.toString();StringBuilder sb = new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while ((line = br.readLine()) != null) {
sb.append(line);
}
String str = sb.toString();
return str;String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining(System.lineSeparator()));String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().parallel().collect(Collectors.joining(System.lineSeparator()));6、使用 Stream API 或 parallel Stream API
String myString = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));或
String myString = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));7、使用StringWriter和IOUtils.copy (Apache Commons)
StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name()); return writer.toString();
甚至可以直接這樣用
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
8、使用CharStreams(Google Guava)
String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
//方法十二: String str = new String(ByteStreams.toByteArray(inputStream))
分別按照字符串長(zhǎng)度來(lái)進(jìn)行測(cè)試。
當(dāng)使用的是一個(gè)小字符串(length=175),得到的性能測(cè)試結(jié)果如下:

當(dāng)使用的是一個(gè)長(zhǎng)字符串(length=50100),得到的性能測(cè)試結(jié)果如下:

為了更加直觀,按照字符串的長(zhǎng)度與相應(yīng)函數(shù)消耗的平均時(shí)間,做了如下的表格:

更加直觀的表格圖,如下:

9、JDK原生提供
byte[] bytes = new byte[0]; bytes = new byte[inputStream.available()]; inputStream.read(bytes); String str = new String(bytes);
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String str = s.hasNext() ? s.next() : "";String resource = new Scanner(inputStream).useDelimiter("\\Z").next();
return resource;2、String轉(zhuǎn)化為InputStream
2.1 JDK原生提供
InputStream is = new ByteArrayInputStream(str.getBytes());
2.2 Apache Common提供
InputStream targetStream = IOUtils.toInputStream(str, StandardCharsets.UTF_8.name());
2.3 Google Guava提供
InputStream targetStream =
new ReaderInputStream(CharSource.wrap(str).openStream(), StandardCharsets.UTF_8.name());到此這篇關(guān)于Java中InputSteam轉(zhuǎn)String的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Java InputSteam轉(zhuǎn)String內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下
相關(guān)文章
在 Spring Boot 項(xiàng)目中使用分頁(yè)插件的兩種常見(jiàn)方式示例詳解
本文介紹了SpringBoot項(xiàng)目中兩種分頁(yè)插件的使用方法:MyBatis-Plus分頁(yè)插件和PageHelper插件,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-10-10
Java注解機(jī)制之Spring自動(dòng)裝配實(shí)現(xiàn)原理詳解
這篇文章主要為大家詳細(xì)介紹了Java注解機(jī)制之Spring自動(dòng)裝配實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Springboot使用pdfbox提取PDF圖片的代碼示例
PDFBox是一個(gè)用于創(chuàng)建和處理PDF文檔的Java庫(kù),它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容,本文就給大家介紹Springboot如何使用pdfbox提取PDF圖片,感興趣的同學(xué)可以借鑒參考2023-06-06
Java Flyway與Liquibase在ORM項(xiàng)目中的應(yīng)用方式
這篇文章主要介紹了Java Flyway與Liquibase在ORM項(xiàng)目中的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Springboot接收Get參數(shù)實(shí)踐過(guò)程
本文主要介紹了在Spring Boot中如何接收不同類型的請(qǐng)求參數(shù),包括在路徑中直接傳遞參數(shù)、跟在問(wèn)號(hào)后面?zhèn)鬟f參數(shù)、使用Map接收參數(shù)、接收數(shù)組以及使用對(duì)象接收參數(shù)等方法2024-12-12
java Swing實(shí)現(xiàn)選項(xiàng)卡功能(JTabbedPane)實(shí)例代碼
這篇文章主要介紹了java Swing實(shí)現(xiàn)選項(xiàng)卡功能(JTabbedPane)實(shí)例代碼的相關(guān)資料,學(xué)習(xí)java 基礎(chǔ)的朋友可以參考下這個(gè)簡(jiǎn)單示例,需要的朋友可以參考下2016-11-11
mybatis使用mapper代理開(kāi)發(fā)方式
使用MyBatis代理開(kāi)發(fā)模式時(shí),需要注意定義與映射配置文件同名的接口類,確保namespace屬性與接口路徑一致,接口方法名和映射文件中的id名稱相同,返回類型保持一致,在mybatis-config.xml中配置映射文件路徑,保證結(jié)構(gòu)一致,可通過(guò)注解@Param傳遞多個(gè)參數(shù)2024-10-10

