如何在Java中读取/转换输入流为字符串?

如果您有一个java.io.InputStream对象,您应该如何处理该对象并生成String


假设我有一个包含文本数据的InputStream,我想将其转换为String,因此我可以将其写入日志文件。

InputStream转换为String的最简单方法是什么?

public String convertStreamToString(InputStream is) {// ???}
2521341 次浏览

一个很好的方法是使用apache公共资源IOUtilsInputStream复制到StringWriter…类似

StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, encoding);String theString = writer.toString();

甚至

// NB: does not close inputStream, you'll have to use try-with-resources for thatString theString = IOUtils.toString(inputStream, encoding);

或者,如果您不想混合您的Streams和Writers,您可以使用ByteArrayOutputStream

考虑到文件,应该首先获得一个java.io.Reader实例。然后可以读取并添加到StringBuilder(如果我们不是在多线程中访问它,我们不需要StringBufferStringBuilder更快)。这里的诀窍是我们在块中工作,因此不需要其他缓冲流。块大小是参数化的,用于运行时性能优化。

public static String slurp(final InputStream is, final int bufferSize) {final char[] buffer = new char[bufferSize];final StringBuilder out = new StringBuilder();try (Reader in = new InputStreamReader(is, "UTF-8")) {for (;;) {int rsz = in.read(buffer, 0, buffer.length);if (rsz < 0)break;out.append(buffer, 0, rsz);}}catch (UnsupportedEncodingException ex) {/* ... */}catch (IOException ex) {/* ... */}return out.toString();}

Apache Commons允许:

String myString = IOUtils.toString(myInputStream, "UTF-8");

当然,您可以选择UTF-8以外的其他字符编码。

另见:(留档

用途:

import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.IOException;
public static String readInputStreamAsString(InputStream in)throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);ByteArrayOutputStream buf = new ByteArrayOutputStream();int result = bis.read();while(result != -1) {byte b = (byte)result;buf.write(b);result = bis.read();}return buf.toString();}

如果您不能使用Commons IO(FileUtils/IOUtils/CopyUtils),下面是一个使用BufferedReader逐行读取文件的示例:

public class StringFromFile {public static void main(String[] args) /*throws UnsupportedEncodingException*/ {InputStream is = StringFromFile.class.getResourceAsStream("file.txt");BufferedReader br = new BufferedReader(new InputStreamReader(is/*, "UTF-8"*/));final int CHARS_PER_PAGE = 5000; //counting spacesStringBuilder builder = new StringBuilder(CHARS_PER_PAGE);try {for(String line=br.readLine(); line!=null; line=br.readLine()) {builder.append(line);builder.append('\n');}}catch (IOException ignore) { }
String text = builder.toString();System.out.println(text);}}

或者,如果您想要原始速度,我会提出Paul de Vrieze建议的变体(避免使用StringWriter(在内部使用StringBuffer):

public class StringFromFileFast {public static void main(String[] args) /*throws UnsupportedEncodingException*/ {InputStream is = StringFromFileFast.class.getResourceAsStream("file.txt");InputStreamReader input = new InputStreamReader(is/*, "UTF-8"*/);final int CHARS_PER_PAGE = 5000; //counting spacesfinal char[] buffer = new char[CHARS_PER_PAGE];StringBuilder output = new StringBuilder(CHARS_PER_PAGE);try {for(int read = input.read(buffer, 0, buffer.length);read != -1;read = input.read(buffer, 0, buffer.length)) {output.append(buffer, 0, read);}} catch (IOException ignore) { }
String text = output.toString();System.out.println(text);}}

如果您使用的是Google-Collection/Guava,您可以执行以下操作:

InputStream stream = ...String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));Closeables.closeQuietly(stream);

请注意,InputStreamReader的第二个参数(即Charsets.UTF_8)不是必需的,但如果您知道它(您应该!)

这是一种仅使用标准Java库的方法(请注意,流未关闭,因人而异)。

static String convertStreamToString(java.io.InputStream is) {java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");return s.hasNext() ? s.next() : "";}

我从“愚蠢的扫描仪技巧”文章中学到了这个技巧。它起作用的原因是扫描仪迭代流中的标记,在这种情况下,我们使用“输入边界的开始”(\A)分离标记,从而为流的整个内容只给我们一个标记。

请注意,如果您需要具体了解输入流的编码,您可以向Scanner构造函数提供第二个参数,该参数指示要使用的字符集(例如“UTF-8”)。

帽子提示也适用于雅各布,他曾经向我指出上述文章。

用途:

InputStream in = /* Your InputStream */;StringBuilder sb = new StringBuilder();BufferedReader br = new BufferedReader(new InputStreamReader(in));String read;
while ((read=br.readLine()) != null) {//System.out.println(read);sb.append(read);}
br.close();return sb.toString();

我做了一些时间测试,因为时间很重要,总是。

我尝试以3种不同的方式将响应转换为String。(如下所示)
为了易读性,我省略了try/catch块。

为了给出上下文,这是所有3种方法的前面代码:

   String response;String url = "www.blah.com/path?key=value";GetMethod method = new GetMethod(url);int status = client.executeMethod(method);

1)

 response = method.getResponseBodyAsString();

2)

InputStream resp = method.getResponseBodyAsStream();InputStreamReader is=new InputStreamReader(resp);BufferedReader br=new BufferedReader(is);String read = null;StringBuffer sb = new StringBuffer();while((read = br.readLine()) != null) {sb.append(read);}response = sb.toString();

3)

InputStream iStream  = method.getResponseBodyAsStream();StringWriter writer = new StringWriter();IOUtils.copy(iStream, writer, "UTF-8");response = writer.toString();

所以,在使用相同的请求/响应数据对每种方法运行了500次测试后,这里是数字。再次,这些是我的发现,你的发现可能不完全相同,但我写这篇文章是为了向其他人表明这些方法的效率差异。

排名:
方法#1
方法#3-比#1慢2.6%
方法#2-比#1慢4.3%

这些方法中的任何一种都是获取响应并从中创建字符串的合适解决方案。

如果你喜欢冒险,你可以混合Scala和Java,最后得到这样的结果:

scala.io.Source.fromInputStream(is).mkString("")

混合Java和Scala代码和库有它的好处。

查看完整描述:在Scala中将InputStream转换为String的惯用方法

这是或多或少sampath的答案,清理了一下并表示为一个函数:

String streamToString(InputStream in) throws IOException {StringBuilder out = new StringBuilder();BufferedReader br = new BufferedReader(new InputStreamReader(in));for(String line = br.readLine(); line != null; line = br.readLine())out.append(line);br.close();return out.toString();}

这是最好的纯Java解决方案,非常适合Android和任何其他JVM。

这个解决方案工作得非常好……它简单、快速,并且在小型和大型流上都能正常工作!!(参见上面的基准测试…8号

public String readFullyAsString(InputStream inputStream, String encoding)throws IOException {return readFully(inputStream).toString(encoding);}
public byte[] readFullyAsBytes(InputStream inputStream)throws IOException {return readFully(inputStream).toByteArray();}
private ByteArrayOutputStream readFully(InputStream inputStream)throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = 0;while ((length = inputStream.read(buffer)) != -1) {baos.write(buffer, 0, length);}return baos;}

快速和容易:

String result = (String)new ObjectInputStream( inputStream ).readObject();

下面的代码对我有用。

URL url = MyClass.class.getResource("/" + configFileName);BufferedInputStream bi = (BufferedInputStream) url.getContent();byte[] buffer = new byte[bi.available() ];int bytesRead = bi.read(buffer);String out = new String(buffer);

请注意,根据Java文档,available()方法可能不适用于InputStream,但始终适用于BufferedInputStream。如果你不想使用available()方法,我们可以使用下面的代码

URL url = MyClass.class.getResource("/" + configFileName);BufferedInputStream bi = (BufferedInputStream) url.getContent();File f = new File(url.getPath());byte[] buffer = new byte[ (int) f.length()];int bytesRead = bi.read(buffer);String out = new String(buffer);

我不确定是否会有任何编码问题。请评论,如果代码有任何问题。

以下是如何使用仅使用字节数组缓冲区的JDK来完成它。这实际上是Commons-ioIOUtils.copy()方法的所有工作方式。如果您从Reader而不是InputStream复制,您可以将byte[]替换为char[]

import java.io.ByteArrayOutputStream;import java.io.InputStream;
...
InputStream is = ....ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);byte[] buffer = new byte[8192];int count = 0;try {while ((count = is.read(buffer)) != -1) {baos.write(buffer, 0, count);}}finally {try {is.close();}catch (Exception ignore) {}}
String charset = "UTF-8";String inputStreamAsString = baos.toString(charset);

如果您使用Stream Readers,请确保在结束时关闭流

private String readStream(InputStream iStream) throws IOException {//build a Stream Reader, it can read char by charInputStreamReader iStreamReader = new InputStreamReader(iStream);//build a buffered Reader, so that i can read whole line at onceBufferedReader bReader = new BufferedReader(iStreamReader);String line = null;StringBuilder builder = new StringBuilder();while((line = bReader.readLine()) != null) {  //Read till endbuilder.append(line);builder.append("\n"); // append new line to preserve lines}bReader.close();         //close all opened stuffiStreamReader.close();//iStream.close(); //EDIT: Let the creator of the stream close it!// some readers may auto close the inner streamreturn builder.toString();}

编辑:在JDK 7+上,您可以使用try-with-资源构造。

/*** Reads the stream into a string* @param iStream the input stream* @return the string read from the stream* @throws IOException when an IO error occurs*/private String readStream(InputStream iStream) throws IOException {
//Buffered reader allows us to read line by linetry (BufferedReader bReader =new BufferedReader(new InputStreamReader(iStream))){StringBuilder builder = new StringBuilder();String line;while((line = bReader.readLine()) != null) {  //Read till endbuilder.append(line);builder.append("\n"); // append new line to preserve lines}return builder.toString();}}

这是我经过一些实验后想出的最优雅、Java(没有库)的解决方案:

public static String fromStream(InputStream in) throws IOException{BufferedReader reader = new BufferedReader(new InputStreamReader(in));StringBuilder out = new StringBuilder();String newLine = System.getProperty("line.separator");String line;while ((line = reader.readLine()) != null) {out.append(line);out.append(newLine);}return out.toString();}

嗯,你可以自己编程…这并不复杂…

String Inputstream2String (InputStream is) throws IOException{final int PKG_SIZE = 1024;byte[] data = new byte [PKG_SIZE];StringBuilder buffer = new StringBuilder(PKG_SIZE * 10);int size;
size = is.read(data, 0, data.length);while (size > 0){String str = new String(data, 0, size);buffer.append(str);size = is.read(data, 0, data.length);}return buffer.toString();}
  InputStream IS=new URL("http://www.petrol.si/api/gas_prices.json").openStream();
ByteArrayOutputStream BAOS=new ByteArrayOutputStream();IOUtils.copy(IS, BAOS);String d= new String(BAOS.toByteArray(),"UTF-8");
System.out.println(d);
InputStreamReader i = new InputStreamReader(s);BufferedReader str = new BufferedReader(i);String msg = str.readLine();System.out.println(msg);

这是您的InputStream对象,它将被转换为String

关闭流并仍抛出IOException的JDK 7/8答案:

StringBuilder build = new StringBuilder();byte[] buf = new byte[1024];int length;try (InputStream is = getInputStream()) {while ((length = is.read(buf)) != -1) {build.append(new String(buf, 0, length));}}

您可以使用Apache Commons。

在IOUtils中,您可以找到具有三个有用实现的toString方法。

public static String toString(InputStream input) throws IOException {return toString(input, Charset.defaultCharset());}
public static String toString(InputStream input) throws IOException {return toString(input, Charset.defaultCharset());}
public static String toString(InputStream input, String encoding)throws IOException {return toString(input, Charsets.toCharset(encoding));}

试试这四种说法。

根据Fred回忆的观点,不建议使用+=运算符附加String,因为每次将新的char附加到现有的String时,都会再次创建一个新的String对象并将其地址分配给st,而旧的st对象变成垃圾。

public String convertStreamToString(InputStream is){int k;StringBuffer sb=new StringBuffer();while((k=fin.read()) != -1){sb.append((char)k);}return sb.toString();}

不推荐,但这也是一种方法

public String convertStreamToString(InputStream is) {int k;String st="";while((k=is.read()) != -1){st+=(char)k;}return st;}

此代码片段位于\sdk\samples\android-19\连接\NetworkConnect\NetworkConnectSample\src\main\java\com\example\android\networkConnect\MainActivity.java这是根据Apache许可证2.0版授权的,由Google编写。

/** Reads an InputStream and converts it to a String.* @param stream InputStream containing HTML from targeted site.* @param len Length of string that this method returns.* @return String concatenated according to len parameter.* @throws java.io.IOException* @throws java.io.UnsupportedEncodingException*/private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {Reader reader = null;reader = new InputStreamReader(stream, "UTF-8");char[] buffer = new char[len];reader.read(buffer);return new String(buffer);}

我已经写了一个这样做的类,所以我想我应该和每个人分享它。有时你不想仅仅为了一件事而添加Apache Commons,而是想要一些比Scanner更愚蠢的东西,它不检查内容。

用法如下

// Read from InputStreamString data = new ReaderSink(inputStream, Charset.forName("UTF-8")).drain();
// Read from Filedata = new ReaderSink(file, Charset.forName("UTF-8")).drain();
// Drain input stream to consolenew ReaderSink(inputStream, Charset.forName("UTF-8")).drainTo(System.out);

以下是ReaderSink的代码:

import java.io.*;import java.nio.charset.Charset;
/*** A simple sink class that drains a {@link Reader} to a {@link String} or* to a {@link Writer}.** @author Ben Barkay* @version 2/20/2014*/public class ReaderSink {/*** The default buffer size to use if no buffer size was specified.*/public static final int DEFAULT_BUFFER_SIZE = 1024;
/*** The {@link Reader} that will be drained.*/private final Reader in;
/*** Constructs a new {@code ReaderSink} for the specified file and charset.* @param file      The file to read from.* @param charset   The charset to use.* @throws FileNotFoundException    If the file was not found on the filesystem.*/public ReaderSink(File file, Charset charset) throws FileNotFoundException {this(new FileInputStream(file), charset);}
/*** Constructs a new {@code ReaderSink} for the specified {@link InputStream}.* @param in        The {@link InputStream} to drain.* @param charset   The charset to use.*/public ReaderSink(InputStream in, Charset charset) {this(new InputStreamReader(in, charset));}
/*** Constructs a new {@code ReaderSink} for the specified {@link Reader}.* @param in    The reader to drain.*/public ReaderSink(Reader in) {this.in = in;}
/*** Drains the data from the underlying {@link Reader}, returning a {@link String} containing* all of the read information. This method will use {@link #DEFAULT_BUFFER_SIZE} for* its buffer size.* @return  A {@link String} containing all of the information that was read.*/public String drain() throws IOException {return drain(DEFAULT_BUFFER_SIZE);}
/*** Drains the data from the underlying {@link Reader}, returning a {@link String} containing* all of the read information.* @param bufferSize    The size of the buffer to use when reading.* @return  A {@link String} containing all of the information that was read.*/public String drain(int bufferSize) throws IOException {StringWriter stringWriter = new StringWriter();drainTo(stringWriter, bufferSize);return stringWriter.toString();}
/*** Drains the data from the underlying {@link Reader}, writing it to the* specified {@link Writer}. This method will use {@link #DEFAULT_BUFFER_SIZE} for* its buffer size.* @param out   The {@link Writer} to write to.*/public void drainTo(Writer out) throws IOException {drainTo(out, DEFAULT_BUFFER_SIZE);}
/*** Drains the data from the underlying {@link Reader}, writing it to the* specified {@link Writer}.* @param out           The {@link Writer} to write to.* @param bufferSize    The size of the buffer to use when reader.*/public void drainTo(Writer out, int bufferSize) throws IOException {char[] buffer = new char[bufferSize];int read;while ((read = in.read(buffer)) > -1) {out.write(buffer, 0, read);}}}

这是在不使用任何第三方库的情况下将InputStream转换为String的完整方法。单线程环境使用StringBuilder,否则使用StringBuffer

public static String getString( InputStream is) throws IOException {int ch;StringBuilder sb = new StringBuilder();while((ch = is.read()) != -1)sb.append((char)ch);return sb.toString();}

我有log4j可用,所以我能够使用org.apache.log4j.lf5.util.StreamUtils.get字节来获取字节,我能够使用String ctor将其转换为字符串

String result = new String(StreamUtils.getBytes(inputStream));

这个很好,因为:

  • 它安全地处理Charset。
  • 您控制读取缓冲区大小。
  • 您可以预配生成器的长度,它不必是一个精确的值。
  • 不受库依赖。
  • Java7或更高。

怎么做呢?

public static String convertStreamToString(InputStream is) throws IOException {StringBuilder sb = new StringBuilder(2048); // Define a size if you have an idea of it.char[] read = new char[128]; // Your buffer size.try (InputStreamReader ir = new InputStreamReader(is, StandardCharsets.UTF_8)) {for (int i; -1 != (i = ir.read(read)); sb.append(read, 0, i));}return sb.toString();}

对于JDK 9

public static String inputStreamString(InputStream inputStream) throws IOException {try (inputStream) {return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);}}

我会使用一些Java技巧。

public static String streamToString(final InputStream inputStream) throws Exception {// buffering optionaltry(final BufferedReader br= new BufferedReader(new InputStreamReader(inputStream))) {// parallel optionalreturn br.lines().parallel().collect(Collectors.joining("\n"));} catch (final IOException e) {throw new RuntimeException(e);// whatever.}}

基本上与其他一些答案相同,除了更简洁。

这是一个改编自org.apache.commons.io.IOUtils源代码的答案,适用于那些想要apache实现但不想要整个库的人。

private static final int BUFFER_SIZE = 4 * 1024;
public static String inputStreamToString(InputStream inputStream, String charsetName)throws IOException {StringBuilder builder = new StringBuilder();InputStreamReader reader = new InputStreamReader(inputStream, charsetName);char[] buffer = new char[BUFFER_SIZE];int length;while ((length = reader.read(buffer)) != -1) {builder.append(buffer, 0, length);}return builder.toString();}
InputStream is = Context.openFileInput(someFileName); // whatever format you have
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[8192];for (int bytesRead; (bytesRead = is.read(b)) != -1;) {bos.write(b, 0, bytesRead);}
String output = bos.toString(someEncoding);

以下内容并没有回答最初的问题,而是回答了一些问题。

几个回答建议形式的循环

String line = null;while((line = reader.readLine()) != null) {// ...}

for(String line = reader.readLine(); line != null; line = reader.readLine()) {// ...}

第一种形式通过在封闭作用域中声明一个变量“read”来污染封闭作用域的命名空间,该变量不会用于for循环之外的任何内容。第二种形式复制readline()调用。

这是一种在Java中编写这种循环的更简洁的方法。事实证明,for循环中的第一个子句不需要实际的初始化器值。这将变量“line”的范围保持在for循环的主体内。更优雅!我还没有看到有人在任何地方使用这种形式(几年前的一天我随机发现了它),但我一直在使用它。

for (String line; (line = reader.readLine()) != null; ) {//...}

静态编程语言用户只需:

println(InputStreamReader(is).readText())

readText()

是静态编程语言标准库的内置扩展方法。

使用的纯Java解决方案,从Java8开始工作。

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.stream.Collectors;
// ...public static String inputStreamToString(InputStream is) throws IOException {try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {return br.lines().collect(Collectors.joining(System.lineSeparator()));}}

正如ChristofleHammarström在其他答案下面提到的那样,显式指定字符集更安全。即InputStreamReader构造函数可以更改如下:

new InputStreamReader(is, Charset.forName("UTF-8"))

番石榴提供当输入流来自类路径资源(这似乎是流行的任务)时,更短的高效自动关闭解决方案:

byte[] bytes = Resources.toByteArray(classLoader.getResource(path));

String text = Resources.toString(classLoader.getResource(path), StandardCharsets.UTF_8);

还有字节源CharSource的一般概念,可以温和地处理打开和关闭流。

因此,例如,不要显式打开一个小文件来读取其内容:

String content = Files.asCharSource(new File("robots.txt"), StandardCharsets.UTF_8).read();byte[] data = Files.asByteSource(new File("favicon.ico")).read();

或者只是

String content = Files.toString(new File("robots.txt"), StandardCharsets.UTF_8);byte[] data = Files.toByteArray(new File("favicon.ico"));

这是我基于Java8的解决方案,它使用新流APIInputStream收集所有行:

public static String toString(InputStream inputStream) {BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));return reader.lines().collect(Collectors.joining(System.getProperty("line.separator")));}

为了完整性,这里是Java9解决方案:

public static String toString(InputStream input) throws IOException {return new String(input.readAllBytes(), StandardCharsets.UTF_8);}

这使用添加到Java9的#0方法。

注意:这可能不是一个好主意。此方法使用递归,因此会很快达到StackOverflowError

public String read (InputStream is) {byte next = is.read();return next == -1 ? "" : next + read(is); // Recursive part: reads next byte recursively}

基于接受的Apache Commons答案的第二部分,但填充了总是关闭流的小间隙:

    String theString;try {theString = IOUtils.toString(inputStream, encoding);} finally {IOUtils.closeQuietly(inputStream);}

reduceconcat而言,它可以在Java8中表示为:

String fromFile = new BufferedReader(newInputStreamReader(inputStream)).lines().reduce(String::concat).get();

使用Java9中支持的java.io.InputStream.transfer到(输出流)和采用字符集名称的ByteArrayOutputStream.toString(String)

public static String gobble(InputStream in, String charsetName) throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream();in.transferTo(bos);return bos.toString(charsetName);}

总结其他答案我找到了11种主要方法来做到这一点(见下文)。我写了一些性能测试(见下面的结果):

将InputStream转换为String的方法:

  1. 使用IOUtils.toString(Apache Utils)

     String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
  2. 使用CharStreams(番石榴)

     String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
  3. 使用Scanner(JDK)

     Scanner s = new Scanner(inputStream).useDelimiter("\\A");String result = s.hasNext() ? s.next() : "";
  4. 使用流式接口(Java8)。警告:此解决方案将不同的换行符(如\r\n)转换为\n

     String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
  5. 使用并行流API(Java8)。警告:此解决方案将不同的换行符(如\r\n)转换为\n

     String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));
  6. 使用InputStreamReaderStringBuilder(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();
  7. 使用StringWriterIOUtils.copy(Apache Commons)

     StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, "UTF-8");return writer.toString();
  8. 使用ByteArrayOutputStreaminputStream.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");
  9. 使用BufferedReader(JDK)。警告:此解决方案将不同的换行符(如\n\r)转换为line.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();
  10. 使用BufferedInputStreamByteArrayOutputStream(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");
  11. 使用inputStream.read()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将不同的换行符转换为一个。

  2. 解决方案11无法正确使用Unicode文本

性能测试

String(长度=175)的性能测试,URL在github(模式=平均时间,系统=Linux,得分1,343是最好的):

              Benchmark                         Mode  Cnt   Score   Error  Units8. ByteArrayOutputStream and read (JDK)        avgt   10   1,343 ± 0,028  us/op6. InputStreamReader and StringBuilder (JDK)   avgt   10   6,980 ± 0,404  us/op10. BufferedInputStream, ByteArrayOutputStream  avgt   10   7,437 ± 0,735  us/op11. InputStream.read() and StringBuilder (JDK)  avgt   10   8,977 ± 0,328  us/op7. StringWriter and IOUtils.copy (Apache)      avgt   10  10,613 ± 0,599  us/op1. IOUtils.toString (Apache Utils)             avgt   10  10,605 ± 0,527  us/op3. Scanner (JDK)                               avgt   10  12,083 ± 0,293  us/op2. CharStreams (guava)                         avgt   10  12,999 ± 0,514  us/op4. Stream Api (Java 8)                         avgt   10  15,811 ± 0,605  us/op9. BufferedReader (JDK)                        avgt   10  16,038 ± 0,711  us/op5. parallel Stream Api (Java 8)                avgt   10  21,544 ± 0,583  us/op

性能测试大String(长度=50100),URL在github(模式=平均时间,系统=Linux,得分200,715是最好的):

               Benchmark                        Mode  Cnt   Score        Error  Units8. ByteArrayOutputStream and read (JDK)        avgt   10   200,715 ±   18,103  us/op1. IOUtils.toString (Apache Utils)             avgt   10   300,019 ±    8,751  us/op6. InputStreamReader and StringBuilder (JDK)   avgt   10   347,616 ±  130,348  us/op7. StringWriter and IOUtils.copy (Apache)      avgt   10   352,791 ±  105,337  us/op2. CharStreams (guava)                         avgt   10   420,137 ±   59,877  us/op9. BufferedReader (JDK)                        avgt   10   632,028 ±   17,002  us/op5. parallel Stream Api (Java 8)                avgt   10   662,999 ±   46,199  us/op4. Stream Api (Java 8)                         avgt   10   701,269 ±   82,296  us/op10. BufferedInputStream, ByteArrayOutputStream  avgt   10   740,837 ±    5,613  us/op3. Scanner (JDK)                               avgt   10   751,417 ±   62,026  us/op11. InputStream.read() and StringBuilder (JDK)  avgt   10  2919,350 ± 1101,942  us/op

图形(Windows 7系统中取决于输入流长度的性能测试)
输入图片描述

性能测试(平均时间)取决于Windows 7系统中的输入流长度:

 length  182    546     1092    3276    9828    29484   58968
test8  0.38    0.938   1.868   4.448   13.412  36.459  72.708test4  2.362   3.609   5.573   12.769  40.74   81.415  159.864test5  3.881   5.075   6.904   14.123  50.258  129.937 166.162test9  2.237   3.493   5.422   11.977  45.98   89.336  177.39test6  1.261   2.12    4.38    10.698  31.821  86.106  186.636test7  1.601   2.391   3.646   8.367   38.196  110.221 211.016test1  1.529   2.381   3.527   8.411   40.551  105.16  212.573test3  3.035   3.934   8.606   20.858  61.571  118.744 235.428test2  3.136   6.238   10.508  33.48   43.532  118.044 239.481test10 1.593   4.736   7.527   20.557  59.856  162.907 323.147test11 3.913   11.506  23.26   68.644  207.591 600.444 1211.545

将inputStream转换为String的方法

public static String getStringFromInputStream(InputStream inputStream) {
BufferedReader bufferedReader = null;StringBuilder stringBuilder = new StringBuilder();String line;
try {bufferedReader = new BufferedReader(new InputStreamReader(inputStream));while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line);}} catch (IOException e) {logger.error(e.getMessage());} finally {if (bufferedReader != null) {try {bufferedReader.close();} catch (IOException e) {logger.error(e.getMessage());}}}return stringBuilder.toString();}
InputStream  inputStream = null;BufferedReader bufferedReader = null;try {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));String stringBuilder = new StringBuilder();String content;while((content = bufferedReader.readLine()) != null){stringBuilder.append(content);}System.out.println("content of file::" + stringBuilder.toString());}catch (IOException e) {e.printStackTrace();}finally{if(bufferedReader != null){try{bufferedReader.close();}catch(IoException ex){ex.printStackTrace();}

您也可以从指定的资源路径获取InputStream:

public static InputStream getResourceAsStream(String path){InputStream myiInputStream = ClassName.class.getResourceAsStream(path);if (null == myiInputStream){mylogger.info("Can't find path = ", path);}
return myiInputStream;}

要从特定路径获取InputStream:

public static URL getResource(String path){URL myURL = ClassName.class.getResource(path);if (null == myURL){mylogger.info("Can't find resource path = ", path);}return myURL;}

另一个,对于所有Spring用户:

import java.nio.charset.StandardCharsets;import org.springframework.util.FileCopyUtils;
public String convertStreamToString(InputStream is) throws IOException {return new String(FileCopyUtils.copyToByteArray(is), StandardCharsets.UTF_8);}

org.springframework.util.StreamUtils中的实用程序方法类似于FileCopyUtils中的实用程序方法,但它们在完成后保持流打开。

JDK中最简单的方法是使用以下代码片段。

String convertToString(InputStream in){String resource = new Scanner(in).useDelimiter("\\Z").next();return resource;}
public String read(InputStream in) throws IOException {try (BufferedReader buffer = new BufferedReader(new InputStreamReader(in))) {return buffer.lines().collect(Collectors.joining("\n"));}}

在Groovy中

inputStream.getText()

Raghu K Nair是唯一一个使用扫描仪的人。我使用的代码有点不同:

String convertToString(InputStream in){Scanner scanner = new Scanner(in)scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();if (hasInput) {return scanner.next();} else {return null;}
}

关于分隔符:如何在Java扫描仪中使用分隔符?

您可以使用仙人掌

String text = new TextOf(inputStream).asString();

UTF-8编码是默认编码。如果您需要另一个:

String text = new TextOf(inputStream, "UTF-16").asString();

这个问题的解决方案不是最简单的,但由于没有提到NIO流和通道,这里有一个版本,它使用NIO通道和ByteBuffer将流转换为字符串。

public static String streamToStringChannel(InputStream in, String encoding, int bufSize) throws IOException {ReadableByteChannel channel = Channels.newChannel(in);ByteBuffer byteBuffer = ByteBuffer.allocate(bufSize);ByteArrayOutputStream bout = new ByteArrayOutputStream();WritableByteChannel outChannel = Channels.newChannel(bout);while (channel.read(byteBuffer) > 0 || byteBuffer.position() > 0) {byteBuffer.flip();  //make buffer ready for writeoutChannel.write(byteBuffer);byteBuffer.compact(); //make buffer ready for reading}channel.close();outChannel.close();return bout.toString(encoding);}

下面是一个如何使用它的例子:

try (InputStream in = new FileInputStream("/tmp/large_file.xml")) {String x = streamToStringChannel(in, "UTF-8", 1);System.out.println(x);}

这种方法的性能应该适合大文件。

我在这里对14个不同的答案进行了基准测试(抱歉没有提供学分,但有太多重复)。

结果非常令人惊讶。事实证明,Apache票据是最慢的解决方案,ByteArrayOutputStream是最快的解决方案:

所以首先是最好的方法:

public String inputStreamToString(InputStream inputStream) throws IOException {try(ByteArrayOutputStream result = new ByteArrayOutputStream()) {byte[] buffer = new byte[1024];int length;while ((length = inputStream.read(buffer)) != -1) {result.write(buffer, 0, length);}
return result.toString(UTF_8);}}

基准测试结果,20个周期中20 MB随机字节

时间(毫秒)

  • ByteArrayOutputStreamTest: 194流水线接口测试数据
  • NioStream:198
  • Java9ISTransferTo:201
  • Java9ISReadAllBytes: 205//每分钟读一次
  • BufferedInput StreamVsByteArrayOutput Stream: 314//输出视频流的大小
  • ApacheStringWriter2:574//字符串写入器
  • GuavaCharStreams: 589
  • 扫描仪读取器号
  • 扫描仪阅读器:633
  • ApacheStringWriter: 1544应用程序
  • 请求参数StreamApi: Error
  • 错误信息ParallelStreamApi: Error
  • 错误信息
  • 错误信息InputStreamAndStringBuilder: Error

基准源代码

import com.google.common.io.CharStreams;import org.apache.commons.io.IOUtils;
import java.io.*;import java.nio.ByteBuffer;import java.nio.channels.Channels;import java.nio.channels.ReadableByteChannel;import java.nio.channels.WritableByteChannel;import java.util.Arrays;import java.util.List;import java.util.Random;import java.util.stream.Collectors;
/*** Created by Ilya Gazman on 2/13/18.*/public class InputStreamToString {

private static final String UTF_8 = "UTF-8";
public static void main(String... args) {log("App started");byte[] bytes = new byte[1024 * 1024];new Random().nextBytes(bytes);log("Stream is ready\n");
try {test(bytes);} catch (IOException e) {e.printStackTrace();}}
private static void test(byte[] bytes) throws IOException {List<Stringify> tests = Arrays.asList(new ApacheStringWriter(),new ApacheStringWriter2(),new NioStream(),new ScannerReader(),new ScannerReaderNoNextTest(),new GuavaCharStreams(),new StreamApi(),new ParallelStreamApi(),new ByteArrayOutputStreamTest(),new BufferReaderTest(),new BufferedInputStreamVsByteArrayOutputStream(),new InputStreamAndStringBuilder(),new Java9ISTransferTo(),new Java9ISReadAllBytes());
String solution = new String(bytes, "UTF-8");
for (Stringify test : tests) {try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {String s = test.inputStreamToString(inputStream);if (!s.equals(solution)) {log(test.name() + ": Error");continue;}}long startTime = System.currentTimeMillis();for (int i = 0; i < 20; i++) {try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {test.inputStreamToString(inputStream);}}log(test.name() + ": " + (System.currentTimeMillis() - startTime));}}
private static void log(String message) {System.out.println(message);}
interface Stringify {String inputStreamToString(InputStream inputStream) throws IOException;
default String name() {return this.getClass().getSimpleName();}}
static class ApacheStringWriter implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {StringWriter writer = new StringWriter();IOUtils.copy(inputStream, writer, UTF_8);return writer.toString();}}
static class ApacheStringWriter2 implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {return IOUtils.toString(inputStream, UTF_8);}}
static class NioStream implements Stringify {
@Overridepublic String inputStreamToString(InputStream in) throws IOException {ReadableByteChannel channel = Channels.newChannel(in);ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 16);ByteArrayOutputStream bout = new ByteArrayOutputStream();WritableByteChannel outChannel = Channels.newChannel(bout);while (channel.read(byteBuffer) > 0 || byteBuffer.position() > 0) {byteBuffer.flip();  //make buffer ready for writeoutChannel.write(byteBuffer);byteBuffer.compact(); //make buffer ready for reading}channel.close();outChannel.close();return bout.toString(UTF_8);}}
static class ScannerReader implements Stringify {
@Overridepublic String inputStreamToString(InputStream is) throws IOException {java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");return s.hasNext() ? s.next() : "";}}
static class ScannerReaderNoNextTest implements Stringify {
@Overridepublic String inputStreamToString(InputStream is) throws IOException {java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");return s.next();}}
static class GuavaCharStreams implements Stringify {
@Overridepublic String inputStreamToString(InputStream is) throws IOException {return CharStreams.toString(new InputStreamReader(is, UTF_8));}}
static class StreamApi implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));}}
static class ParallelStreamApi implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {return new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));}}
static class ByteArrayOutputStreamTest implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {try(ByteArrayOutputStream result = new ByteArrayOutputStream()) {byte[] buffer = new byte[1024];int length;while ((length = inputStream.read(buffer)) != -1) {result.write(buffer, 0, length);}
return result.toString(UTF_8);}}}
static class BufferReaderTest implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {String newLine = System.getProperty("line.separator");BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));StringBuilder result = new StringBuilder(UTF_8);String line;boolean flag = false;while ((line = reader.readLine()) != null) {result.append(flag ? newLine : "").append(line);flag = true;}return result.toString();}}
static class BufferedInputStreamVsByteArrayOutputStream implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();int result = bis.read();while (result != -1) {buf.write((byte) result);result = bis.read();}
return buf.toString(UTF_8);}}
static class InputStreamAndStringBuilder implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {int ch;StringBuilder sb = new StringBuilder(UTF_8);while ((ch = inputStream.read()) != -1)sb.append((char) ch);return sb.toString();}}
static class Java9ISTransferTo implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream();inputStream.transferTo(bos);return bos.toString(UTF_8);}}
static class Java9ISReadAllBytes implements Stringify {
@Overridepublic String inputStreamToString(InputStream inputStream) throws IOException {return new String(inputStream.readAllBytes(), UTF_8);}}
}

关于Okio:

String result = Okio.buffer(Okio.source(inputStream)).readUtf8();

我已经创建了这个代码,它可以工作。没有必要的外部插件。

有一个转换器StringStreamStreamString

import java.io.ByteArrayInputStream;import java.io.InputStream;
public class STRINGTOSTREAM {
public static void main(String[] args){String text = "Hello Bhola..!\nMy Name Is Kishan ";
InputStream strm = new ByteArrayInputStream(text.getBytes());    // Convert String to Stream
String data = streamTostring(strm);
System.out.println(data);}
static String streamTostring(InputStream stream){String data = "";
try{StringBuilder stringbuld = new StringBuilder();int i;while ((i=stream.read())!=-1){stringbuld.append((char)i);}data = stringbuld.toString();}catch(Exception e){data = "No data Streamed.";}return data;}

ISO-8859-1

如果您知道输入流的编码是ISO-8859-1或ASCII,这里有一个非常高性能的方法来做到这一点。它(1)避免了StringWriter内部StringBuffer中存在的不必要的同步,(2)避免了InputStreamReader的开销,(3)最小化了StringBuilder内部char数组必须复制的次数。

public static String iso_8859_1(InputStream is) throws IOException {StringBuilder chars = new StringBuilder(Math.max(is.available(), 4096));byte[] buffer = new byte[4096];int n;while ((n = is.read(buffer)) != -1) {for (int i = 0; i < n; i++) {chars.append((char)(buffer[i] & 0xFF));}}return chars.toString();}

UTF-8

对于使用UTF-8编码的流,可以使用相同的一般策略:

public static String utf8(InputStream is) throws IOException {StringBuilder chars = new StringBuilder(Math.max(is.available(), 4096));byte[] buffer = new byte[4096];int n;int state = 0;while ((n = is.read(buffer)) != -1) {for (int i = 0; i < n; i++) {if ((state = nextStateUtf8(state, buffer[i])) >= 0) {chars.appendCodePoint(state);} else if (state == -1) { //errorstate = 0;chars.append('\uFFFD'); //replacement char}}}return chars.toString();}

其中nextStateUtf8()函数定义如下:

/*** Returns the next UTF-8 state given the next byte of input and the current state.* If the input byte is the last byte in a valid UTF-8 byte sequence,* the returned state will be the corresponding unicode character (in the range of 0 through 0x10FFFF).* Otherwise, a negative integer is returned. A state of -1 is returned whenever an* invalid UTF-8 byte sequence is detected.*/static int nextStateUtf8(int currentState, byte nextByte) {switch (currentState & 0xF0000000) {case 0:if ((nextByte & 0x80) == 0) { //0 trailing bytes (ASCII)return nextByte;} else if ((nextByte & 0xE0) == 0xC0) { //1 trailing byteif (nextByte == (byte) 0xC0 || nextByte == (byte) 0xC1) { //0xCO & 0xC1 are overlongreturn -1;} else {return nextByte & 0xC000001F;}} else if ((nextByte & 0xF0) == 0xE0) { //2 trailing bytesif (nextByte == (byte) 0xE0) { //possibly overlongreturn nextByte & 0xA000000F;} else if (nextByte == (byte) 0xED) { //possibly surrogatereturn nextByte & 0xB000000F;} else {return nextByte & 0x9000000F;}} else if ((nextByte & 0xFC) == 0xF0) { //3 trailing bytesif (nextByte == (byte) 0xF0) { //possibly overlongreturn nextByte & 0x80000007;} else {return nextByte & 0xE0000007;}} else if (nextByte == (byte) 0xF4) { //3 trailing bytes, possibly undefinedreturn nextByte & 0xD0000007;} else {return -1;}case 0xE0000000: //3rd-to-last continuation bytereturn (nextByte & 0xC0) == 0x80 ? currentState << 6 | nextByte & 0x9000003F : -1;case 0x80000000: //3rd-to-last continuation byte, check overlongreturn (nextByte & 0xE0) == 0xA0 || (nextByte & 0xF0) == 0x90 ? currentState << 6 | nextByte & 0x9000003F : -1;case 0xD0000000: //3rd-to-last continuation byte, check undefinedreturn (nextByte & 0xF0) == 0x80 ? currentState << 6 | nextByte & 0x9000003F : -1;case 0x90000000: //2nd-to-last continuation bytereturn (nextByte & 0xC0) == 0x80 ? currentState << 6 | nextByte & 0xC000003F : -1;case 0xA0000000: //2nd-to-last continuation byte, check overlongreturn (nextByte & 0xE0) == 0xA0 ? currentState << 6 | nextByte & 0xC000003F : -1;case 0xB0000000: //2nd-to-last continuation byte, check surrogatereturn (nextByte & 0xE0) == 0x80 ? currentState << 6 | nextByte & 0xC000003F : -1;case 0xC0000000: //last continuation bytereturn (nextByte & 0xC0) == 0x80 ? currentState << 6 | nextByte & 0x3F : -1;default:return -1;}}

自动检测编码

如果您的输入流是使用ASCII或ISO-8859-1或UTF-8编码的,但您不确定是哪一个,我们可以使用与最后一个类似的方法,但在返回字符串之前使用额外的编码检测组件来自动检测编码。

public static String autoDetect(InputStream is) throws IOException {StringBuilder chars = new StringBuilder(Math.max(is.available(), 4096));byte[] buffer = new byte[4096];int n;int state = 0;boolean ascii = true;while ((n = is.read(buffer)) != -1) {for (int i = 0; i < n; i++) {if ((state = nextStateUtf8(state, buffer[i])) > 0x7F)ascii = false;chars.append((char)(buffer[i] & 0xFF));}}
if (ascii || state < 0) { //probably not UTF-8return chars.toString();}//probably UTF-8int pos = 0;char[] charBuf = new char[2];for (int i = 0, len = chars.length(); i < len; i++) {if ((state = nextStateUtf8(state, (byte)chars.charAt(i))) >= 0) {boolean hi = Character.toChars(state, charBuf, 0) == 2;chars.setCharAt(pos++, charBuf[0]);if (hi) {chars.setCharAt(pos++, charBuf[1]);}}}return chars.substring(0, pos);}

如果您的输入流的编码既不是ISO-8859-1也不是ASCII也不是UTF-8,那么我将推迟到已经存在的其他答案。

我建议StringWriter类解决这个问题。

StringWriter wt= new StringWriter();IOUtils.copy(inputStream, wt, encoding);String st= wt.toString();

此代码适用于新Java学习者:

private String textDataFromFile;
public String getFromFile(InputStream myInputStream) throws FileNotFoundException, IOException {
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(myInputStream));
StringBuilder stringBuilder = new StringBuilder();
String eachStringLine;
while ((eachStringLine = bufferReader.readLine()) != null) {stringBuilder.append(eachStringLine).append("\n");}textDataFromFile = stringBuilder.toString();
return textDataFromFile;}
String inputStreamToString(InputStream inputStream, Charset charset) throws IOException {try (final StringWriter writer = new StringWriter();final InputStreamReader reader = new InputStreamReader(inputStream, charset)) {reader.transferTo(writer);return writer.toString();}}

如果您需要将字符串转换为特定字符集没有外部库然后:

public String convertStreamToString(InputStream is) throws IOException {try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {is.transferTo(baos);return baos.toString(StandardCharsets.UTF_8);}}

最简单的方法一条线

 public static void main(String... args) throws IOException {System.out.println(new String(Files.readAllBytes(Paths.get("csv.txt"))));}

如果您使用的是AWS SDK v2,请致电IoUtils.toUtf8String()

public String convertStreamToString(InputStream is) {return IoUtils.toUtf8String(is);}