龙空技术网

在Java中将输入流读取转换为字符串的11种方法

莱迪娜的风声 447

前言:

此刻同学们对“byte转stringjava”可能比较关怀,咱们都需要了解一些“byte转stringjava”的相关资讯。那么小编在网上汇集了一些对于“byte转stringjava””的相关文章,希望各位老铁们能喜欢,姐妹们快快来学习一下吧!

我找到了11种主要的方法来做到这一点(见下文)。

将输入流转换为字符串的方法:

·使用 IOUtils.toString (Apache Utils)

String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

Maven配置

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>

·使用 CharStreams (Guava)

String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));

Maven 配置

<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>18.0</version></dependency>

·使用 Scanner (JDK)

Scanner s = new Scanner(inputStream).useDelimiter("\\A");String result = s.hasNext() ? s.next() : "";

·使用 Stream API (Java 8). 警告: 此解决方案将不同的换行符(如\r\n)转换为\n。

String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));

·使用 parallel Stream API (Java 8). 警告: 这个解决方案将不同的换行符(如\r\n)转换为\n。

String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));

·使用 InputStreamReader and StringBuilder (JDK)

int bufferSize = 1024;char[] buffer = new char[bufferSize];StringBuilder out = new StringBuilder();Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {out.append(buffer, 0, numRead);}return out.toString();

·使用 StringWriter and IOUtils.copy (Apache Commons)

StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, "UTF-8");return writer.toString();

·使用 ByteArrayOutputStream and inputStream.read (JDK)

ByteArrayOutputStream result = new ByteArrayOutputStream();byte[] buffer = new byte[1024];for (int length; (length = inputStream.read(buffer)) != -1; ) {result.write(buffer, 0, length);}// StandardCharsets.UTF_8.name() > JDK 7return result.toString("UTF-8");

·使用 BufferedReader (JDK). 警告: 这个解决方案将不同的换行符(如\n\r)转换为行。separator系统属性(例如,在Windows中为"\r\n")。

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);}return result.toString();

·使用 BufferedInputStream and ByteArrayOutputStream (JDK)

BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();for (int result = bis.read(); result != -1; result = bis.read()) {buf.write((byte) result);}// StandardCharsets.UTF_8.name() > JDK 7return buf.toString("UTF-8");

·使用 inputStream.read() and StringBuilder (JDK). 警告: 这个解决方案在Unicode方面有问题,只能在非Unicode文本上正确工作

StringBuilder sb = new StringBuilder();for (int ch; (ch = inputStream.read()) != -1; ) {sb.append((char) ch);}return sb.toString();

注意:

1、解决方案4、5、9此解决方案将不同的换行符(如\r\n转换为\n),(\r\n是Windows系统的换行符,而在Unix、Liunx等系统中的换行符为 \n)。

2、解决方案11不能正确地处理Unicode文本

最后是全部总结测试(代码已经经过编写测试时间:2022年9月24日11:20:13):

package eleven.莱迪娜的风声;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringWriter;import java.nio.charset.StandardCharsets;import java.util.Scanner;import java.util.stream.Collectors;import org.apache.commons.io.IOUtils;import com.google.common.base.Charsets;import com.google.common.io.CharStreams;/*** 时间:2022年9月24日11:18:17** @author 莱迪娜的风声*/public class InputStreamToChar {public static void main(String[] args) throws IOException {InputStream inputStream = new FileInputStream(new File("Test.txt"));one(inputStream);two(inputStream);three(inputStream);four(inputStream);five(inputStream);six(inputStream);seven(inputStream);eight(inputStream);nine(inputStream);ten(inputStream);eleven(inputStream);}public static void one(InputStream inputStream) throws IOException {String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);System.out.println(result);}public static void two(InputStream inputStream) throws IOException {String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));System.out.println(result);}public static void three(InputStream inputStream) throws IOException {Scanner s = new Scanner(inputStream).useDelimiter("\\A");String result = s.hasNext() ? s.next() : "";System.out.println(result);}public static void four(InputStream inputStream) throws IOException {String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));System.out.println(result);}public static void five(InputStream inputStream) throws IOException {String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));System.out.println(result);}public static void six(InputStream inputStream) throws IOException {int bufferSize = 1024;char[] buffer = new char[bufferSize];StringBuilder out = new StringBuilder();Reader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0;) {out.append(buffer, 0, numRead);}String result = out.toString();System.out.println(result);}public static void seven(InputStream inputStream) throws IOException {StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, "UTF-8");String result = writer.toString();System.out.println(result);}public static void eight(InputStream inputStream) throws IOException {ByteArrayOutputStream result = new ByteArrayOutputStream();byte[] buffer = new byte[1024];for (int length; (length = inputStream.read(buffer)) != -1;) {result.write(buffer, 0, length);}// StandardCharsets.UTF_8.name() > JDK 7System.out.println(result.toString("UTF-8"));}public static void nine(InputStream inputStream) throws IOException {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);}System.out.println(result.toString());}public static void ten(InputStream inputStream) throws IOException {BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();for (int result = bis.read(); result != -1; result = bis.read()) {buf.write((byte) result);}// StandardCharsets.UTF_8.name() > JDK 7System.out.println(buf.toString("UTF-8"));}public static void eleven(InputStream inputStream) throws IOException {StringBuilder sb = new StringBuilder();for (int ch; (ch = inputStream.read()) != -1;) {sb.append((char) ch);}System.out.println(sb.toString());}}

标签: #byte转stringjava