热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Java中读取/转换InputStream为String?

如果你有一个java.io.InputStream对象,你应该如何处理该对象并产生一个String?假设我有一个

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


假设我有一个InputStream包含文本数据,并且我想将其转换为a String,所以例如我可以将其写入日志文件.

采取InputStream并将其转换为最简单的方法是String什么?

public String convertStreamToString(InputStream is) {
// ???
}

回答



一个很好的方法是使用Apache公共资源 IOUtils将其复制InputStreamStringWriter...类似的东西

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 that
String theString = IOUtils.toString(inputStream, encoding);

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



  • 对于Android开发人员来说,似乎android没有来自Apache的IOUtils.所以你可以考虑参考其他答案.


  • 这是一个令人难以置信的老问题(2008年被问到).值得花时间阅读更现代的答案.有些使用来自Java 8库的本机调用.


  • 这个答案已经过时了,应该能够将其标记为这样(遗憾的是,这不可能是atm).


  • IOUtils.toString()早已被弃用。绝对不再建议使用此答案。


  • 然后[编辑]以说明为什么不推荐使用它来帮助将来的读者。







这是一种仅使用标准Java库的方法(请注意,流未关闭,您的里程可能会有所不同).

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

我从"Stupid Scanner tricks"文章中学到了这个技巧.它工作的原因是因为Scanner迭代流中的标记,在这种情况下我们使用"输入边界的开头"( A)分隔标记,因此只为流的整个内容提供一个标记.

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

帽子小贴士也向Jacob说道,他曾经指着我说过这篇文章.



  • 对于Java 7,您可以在try-with中关闭:`try(java.util.Scanner s = new java.util.Scanner(is)){return s.useDelimiter("\ A").hasNext()?s.next():""; }`


  • FYI,[hasNext](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext())在控制台输入流上阻塞(见[here](http:/ /stackoverflow.com/questions/16873134/hasnext-when-does-it-block-and-why)).(现在刚刚遇到这个问题.)这个解决方案工作正常,否则......只是抬头.


  • 谢谢,对于我的版本,我添加了一个关闭输入流的finally块,因此用户不必阅读输入.大大简化了调用者代码.


  • 不幸的是,这个解决方案似乎失去了我的底层流实现中抛出的异常.


  • 在我的情况下@PavelRepin @Patrick,一个空的inputStream在Scanner构造期间导致了NPE.我必须添加`if(is == null)return"";`在方法的开头; 我相信这个答案需要更新以更好地处理null inputStreams.


  • @earcam感谢您的提示!对于那些想知道它是如何工作的人来说,这要归功于[try-with-resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)







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

将InputStream转换为String的方法:


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


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


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


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


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


  6. final int bufferSize = 1024;
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(inputStream, "UTF-8");
    for (; ; ) {
    int rsz = in.read(buffer, 0, buffer.length);
    if (rsz <0)
    break;
    out.append(buffer, 0, rsz);
    }
    return out.toString();


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


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


  9. String newLine = System.getProperty("line.separator");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder result = new StringBuilder();
    boolean flag = false;
    for (String line; (line = reader.readLine()) != null; ) {
    result.append(flag? newLine: "").append(line);
    flag = true;
    }
    return result.toString();


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


  11. int ch;
    StringBuilder sb = new StringBuilder();
    while((ch = inputStream.read()) != -1)
    sb.append((char)ch);
    reset();
    return sb.toString();


警告:






性能测试

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

Benchmark Mode Cnt Score Error Units
8. ByteArrayOutputStream and read (JDK) avgt 10 1,343 ± 0,028 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 6,980 ± 0,404 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 7,437 ± 0,735 us/op
11. InputStream.read() and StringBuilder (JDK) avgt 10 8,977 ± 0,328 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 10,613 ± 0,599 us/op
1. IOUtils.toString (Apache Utils) avgt 10 10,605 ± 0,527 us/op
3. Scanner (JDK) avgt 10 12,083 ± 0,293 us/op
2. CharStreams (guava) avgt 10 12,999 ± 0,514 us/op
4. Stream Api (Java 8) avgt 10 15,811 ± 0,605 us/op
9. BufferedReader (JDK) avgt 10 16,038 ± 0,711 us/op
5. parallel Stream Api (Java 8) avgt 10 21,544 ± 0,583 us/op

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

Benchmark Mode Cnt Score Error Units
8. ByteArrayOutputStream and read (JDK) avgt 10 200,715 ± 18,103 us/op
1. IOUtils.toString (Apache Utils) avgt 10 300,019 ± 8,751 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 347,616 ± 130,348 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 352,791 ± 105,337 us/op
2. CharStreams (guava) avgt 10 420,137 ± 59,877 us/op
9. BufferedReader (JDK) avgt 10 632,028 ± 17,002 us/op
5. parallel Stream Api (Java 8) avgt 10 662,999 ± 46,199 us/op
4. Stream Api (Java 8) avgt 10 701,269 ± 82,296 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 740,837 ± 5,613 us/op
3. Scanner (JDK) avgt 10 751,417 ± 62,026 us/op
11. 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.708
test4 2.362 3.609 5.573 12.769 40.74 81.415 159.864
test5 3.881 5.075 6.904 14.123 50.258 129.937 166.162
test9 2.237 3.493 5.422 11.977 45.98 89.336 177.39
test6 1.261 2.12 4.38 10.698 31.821 86.106 186.636
test7 1.601 2.391 3.646 8.367 38.196 110.221 211.016
test1 1.529 2.381 3.527 8.411 40.551 105.16 212.573
test3 3.035 3.934 8.606 20.858 61.571 118.744 235.428
test2 3.136 6.238 10.508 33.48 43.532 118.044 239.481
test10 1.593 4.736 7.527 20.557 59.856 162.907 323.147
test11 3.913 11.506 23.26 68.644 207.591 600.444 1211.545



  • It seems this answer is incomplete


  • 惊人的帖子.就一件事.Java 8警告不要在资源上使用并行流来强制你锁定和等待(比如这个输入流),所以并行流选项相当麻烦,不值得吗?


  • 在编写"摘要答案"时,您应该注意到某些解决方案会自动将不同的换行符(例如` r n`)转换为`n`,这在某些情况下可能不受欢迎.也很高兴看到所需的额外内存或至少分配压力(至少你可以用`-prof gc`运行JMH).对于非常酷的帖子,很高兴看到图形(取决于相同输入大小内的字符串长度,并取决于相同字符串长度内的输入大小).


  • Upvoted; 最有趣的是结果超出预期:应该使用标准的JDK和/或Apache Commons语法糖.


  • 并行流实际上是否保持行顺序?


  • 例11中的`reset()`是什么?


  • I converted all the `while` loops to `for` loops in an edit to this post, to avoid polluting the namespace with a variable that isn't used outside the loop. It's a neat trick that works in most Java reader/writer loops.







Apache Commons允许:

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

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

另见:( 文件)



  • 为了节省任何人Googling的麻烦 - org.apache.commons commons-io 1.3.2


  • @GuillaumeCoté我想这里的消息是你永远不应该"使用默认编码",因为你不能确定它是什么,这取决于运行java代码的平台.


  • @Per Wiklander我不同意你的意见.对单个代码起作用的代码可以确定默认编码没问题.对于仅打开本地文件的代码,要求它们以平台默认编码进行编码是合理的选择.


  • 使用apache io(或其他)常量进行字符编码而不是使用普通字符串文字也很少有改进 - 例如:IOUtils.toString(myInputStream,Charsets.UTF_8);







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

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();
}



  • @ User1 - 我喜欢在我的代码中使用库,这样我就可以更快地完成工作.当你的经理们说"哇詹姆斯!你怎么这么快就完成了这件事?"时,真棒.但是,当我们不得不花时间重新发明轮子只是因为我们对包含一个共同的,可重复使用的,经过试验和测试的实用程序的错误想法,我们放弃了时间,我们可以花费进一步的项目目标.当我们重新发明轮子时,我们努力工作两倍,但很晚才到达终点线.一旦我们到达终点线,那里就没有人向我们表示祝贺.在建造房屋时,也不要制造锤子


  • 对不起,在重新阅读我的评论后,它有点傲慢.我认为有充分的理由避免使用库是很重要的,原因是有效的,很可能是:)


  • @ jmort253如果你已经使用了apache commons我会说,去吧.同时,使用库会产生实际成本(因为许多apache java库中的依赖性增长显示).如果这是图书馆的唯一用途,那么使用图书馆就太过分了.另一方面,确定自己的缓冲区大小可以调整内存/处理器使用率平衡.


  • 此解决方案使用多字节字符.该示例使用UTF-8编码,该编码允许表达完整的unicode范围(包括中文).用另一种编码替换"UTF-8"将允许使用该编码.


  • @jmort253在我们的产品中更新了一些库多次后,我们注意到了性能回归.幸运的是,我们正在建立和销售我们自己的产品,所以我们并没有真正拥有所谓的截止日期.不幸的是,我们正在构建一个可在许多操作系统上的许多JVM,数据库和应用服务器上使用的产品,因此我们必须考虑使用不良机器的用户......并且字符串操作优化可以将性能提高30%~40%.还有一个问题:`在我们的产品中,我甚至更换了'应该是'我们甚至更换'.


  • @ jmort253有时候有理由避免使用库.这取决于公司和项目.有时自己的实施正在重新发明轮子 - 然后公司应该使用众所周知的解决方案.但有些情况下你不应该使用someones解决方案:优化,**版权**,架构等.







使用:

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();



  • 此外,readLine不区分n和 r,因此您无法再次重现精确的流.


  • 问题是,你首先分成几行,然后撤消它.只读取任意缓冲区更容易,更快捷.


  • 这不是最好的答案,因为它不是字节输出的严格字节.读者选择换行,所以你必须小心维护它们.


  • 非常低效,因为`readLine`逐个字符地读取EOL.此外,如果流中没有换行符,这实际上没有意义.


  • @Gops AB:如果您尝试这样做,并且示例中包含换行符,您将看到使用readline()和StringBuilder.append()构造此循环的方式实际上并未保留换行符。







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

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

请注意,第二个参数(即Charsets.UTF_8)InputStreamReader不是必需的,但如果您知道它(通常应该!),通常最好指定编码.



  • @harschware:问题是:"如果你有java.io.InputStream对象,你应该如何处理该对象并生成一个String?" 我假设情况中已经存在流.


  • guava为+1,-1为未指定输入流的编码.例如.new InputStreamReader(stream,"UTF-8")







这是我的纯Java和Android解决方案,它运行良好......

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;
}



  • 与仅适用于企业级Java的其他答案相比,在Android上运行良好.


  • 快速注意:它的内存占用量最大为"2*n",其中n是流的大小,根据`ByteArrayInputStream`自动增长系统.


  • 不必要地将内存使用量加倍,这在移动设备上很宝贵.你最好使用InputStreamReader并附加到StringReader,字节到字符的转换将在运行时完成,而不是最后批量.







为了完整起见,这里是Java 9解决方案:

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

readAllBytes目前在JDK 9主要的基本代码,因此它可能出现在释放.您可以使用JDK 9快照构建立即尝试.



  • @ChristianHujer,问题是关于`InputStream`,而不是关于`Path`.`InputStream`可以从许多不同的源创建,而不仅仅是文件.


  • @ChristianHujer,我在[最新的jdk8u提交]中没有看到它(http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/652c6ab45018/src/share/classes/java/io/InputStream的.java).AFAIK新方法从未在Java更新中引入,仅在主要版本中引入.


  • 这是一年前编写的,因此要进行更新,我确认此方法确实在公共发行版JDK 9中。此外,如果您的编码是“ ISO-Latin-1”,那么这将非常有效,因为现在使用Java 9 Strings如果所有字符都位于前256个代码点中,则采用“ byte []”实现。这意味着新的String(byte [],“ ISO-Latin-1”)将是一个简单的数组副本。







使用:

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();
}



  • 这个很慢,因为逐字节读取.


  • @EJP我发现它比使用`BufferedInputStream`*和*读取字节数组缓冲区而不是一次一个字节慢.示例:读取4.56 MiB文件时200ms vs 60ms.







这是我在经过一些实验后想出的最优雅的纯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();
}



  • @TorbenKohlmeier,读者和缓冲区不需要关闭.提供的`InputStream`应该由调用者关闭.


  • 不要忘记提到在InputStreamReader中有一个更优选的构造函数,它接受一个CharSet.


  • 为什么人们继续使用`readLine`?如果你不使用这些线本身,它有什么用处(除非很慢?)


  • 不要用线阅读.如果一条线太长而不适合堆积怎么办?


  • @voho,如果一行是那么长,那么无论如何都无法分配返回值,该值必须等于或大于该行的大小.如果你正在处理那些大的文件,你应该流式传输它们.但是有很多用例可以将小文本文件加载到内存中.


  • @Kong检查其他答案,当您不在乎行时,还有其他方法可以读取一堆数据。







我在这里做了14个不同答案的基准(抱歉没有提供学分,但有太多重复).

结果非常令人惊讶.事实证明,Apache IOUtils是最慢的,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

  • BufferedInputStreamVsByteArrayOutputStream:314

  • ApacheStringWriter2:574

  • GuavaCharStreams:589

  • ScannerReaderNoNextTest:614

  • ScannerReader:633

  • ApacheStringWriter:1544

  • StreamApi:错误

  • ParallelStreamApi:错误

  • BufferReaderTest:错误

  • InputStreamAndStringBuilder:错误


基准源代码

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 readyn");
try {
test(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void test(byte[] bytes) throws IOException {
List 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 {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, UTF_8);
return writer.toString();
}
}
static class ApacheStringWriter2 implements Stringify {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
return IOUtils.toString(inputStream, UTF_8);
}
}
static class NioStream implements Stringify {
@Override
public 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 write
outChannel.write(byteBuffer);
byteBuffer.compact(); //make buffer ready for reading
}
channel.close();
outChannel.close();
return bout.toString(UTF_8);
}
}
static class ScannerReader implements Stringify {
@Override
public 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 {
@Override
public 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 {
@Override
public String inputStreamToString(InputStream is) throws IOException {
return CharStreams.toString(new InputStreamReader(
is, UTF_8));
}
}
static class StreamApi implements Stringify {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
return new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("n"));
}
}
static class ParallelStreamApi implements Stringify {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
return new BufferedReader(new InputStreamReader(inputStream)).lines()
.parallel().collect(Collectors.joining("n"));
}
}
static class ByteArrayOutputStreamTest implements Stringify {
@Override
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);
}
}
}
static class BufferReaderTest implements Stringify {
@Override
public 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 {
@Override
public 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 {
@Override
public 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 {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
inputStream.transferTo(bos);
return bos.toString(UTF_8);
}
}
static class Java9ISReadAllBytes implements Stringify {
@Override
public String inputStreamToString(InputStream inputStream) throws IOException {
return new String(inputStream.readAllBytes(), UTF_8);
}
}
}






我会使用一些Java 8技巧.

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

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



  • 为什么在流上调用`parallel()`?


  • 那个"返回null"会被调用吗?要么`br.lines ...`返回,要么抛出异常.


  • 如果源流使用windows行结尾,这将不会导致数据的_honest_副本,因为所有` r n`最终都会被转换为`n` ...


  • @Khaled A Khunaifer:是的,非常肯定......也许你应该看看这里:http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.你错误编辑的是"尝试资源"声明.


  • 您可以使用`System.lineSeparator()`来使用适当的平台相关的行结尾.







我经常进行一些时间测试,因为时间很重要.

我试图以不同的方式将响应变为String 3.(如下所示)

为了便于阅读,我省略了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 慢2.6%- 比#1慢4.3%

这些方法中的任何一种都是用于获取响应并从中创建String的适当解决方案.



  • 2)包含一个错误,它总是在字符串的末尾添加"null",因为你总是需要再做一步.无论如何,我认为表现将是一样的.这应该工作:String read = null; StringBuffer sb = new StringBuffer(); while((read = br.readLine())!= null){sb.append(read); }







使用Stream的纯Java解决方案,自Java 8开始工作.

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()));
}
}

正如ChristofferHammarström在其他答案中所提到的,明确指定Charset更安全.即InputStreamReader构造函数可以更改如下:

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



  • 而不是`Charset.forName("UTF-8")`,使用`StandardCharsets.UTF_8`(来自`java.nio.charset`).







这里是或多或少的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();
}






如果您喜欢冒险,可以将Scala和Java混合使用,最后得到:

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

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

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



  • 现在只需这样就可以了:`Source.fromInputStream(...).mkString`







如果您不能使用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 spaces
StringBuilder 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 spaces
final 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);
}
}






如果使用Stream Readers,请务必在结尾处关闭流

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


编辑:在JDK 7+上,您可以使用try-with-resources构造.

/**
* 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 line
try (BufferedReader bReader =
new BufferedReader(new InputStreamReader(iStream))){
StringBuilder builder = new StringBuilder();
String line;
while((line = bReader.readLine()) != null) { //Read till end
builder.append(line);
builder.append("n"); // append new line to preserve lines
}
return builder.toString();
}
}






这是一个改编自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();
}






这个很好,因为:


  • Charset的手安全.

  • 您可以控制读取缓冲区大小.

  • 您可以设置构建器的长度,但可能不完全相同.

  • 没有库依赖项.

  • 适用于Java 7或更高版本.

这是为了什么?

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();
}






这是在不使用任何第三方库的情况下转换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();
}



  • 在此方法中,不应用编码.因此,假设从InputStream接收的数据是使用UTF-8编码的,输出将是错误的.要解决这个问题,你可以使用`in = new InputStreamReader(inputStream)`和`(char)in.read()`.


  • 而且记忆效率低下; 我相信我之前在大型输入上尝试使用它,并且StringBuilder内存不足







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();
}
}


  • 纯Java标准库解决方案——没有库

  • 从 Java 10 开始 - Reader#transferTo(java.io.Writer)

  • 无环解决方案

  • 没有换行符处理







以下是使用字节数组缓冲区仅使用JDK的方法.这实际上是commons-io IOUtils.copy()方法的工作原理.您可以替换byte[]使用char[],如果你从一个拷贝Reader,而不是一个InputStream.

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);






Kotlin用户只需:

println(InputStreamReader(is).readText())

readText()

是Kotlin标准库的内置扩展方法.






另一个,适用于所有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,但它们在完成时将流保持打开状态.






使用Java 9中支持的java.io.InputStream.transferTo(OutputStream)和采用charset名称的ByteArrayOutputStream.toString(String):

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






JDK中最简单的方法是使用以下代码snipplet.

String convertToString(InputStream in){
String resource = new Scanner(in).useDelimiter("\Z").next();
return resource;
}






这是我的基于Java 8的解决方案,它使用新的Stream API从以下各项收集所有行InputStream:

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






下面的代码对我有用。

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);

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






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

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();
}






关闭流并仍然抛出 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,并且想要比不检查内容的 Scanner 更愚蠢的东西。

用法如下

// Read from InputStream
String data = new ReaderSink(inputStream, Charset.forName("UTF-8")).drain();
// Read from File
data = new ReaderSink(file, Charset.forName("UTF-8")).drain();
// Drain input stream to console
new 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);
}
}
}






基于已接受的 Apache Commons 答案的第二部分,但填补了小空白以始终关闭流:

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






就...而言reduce,concat它可以用Java 8表示为:

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






在Groovy中

inputStream.getText()



  • 这是理想的结果,无法理解为什么java变得如此棘手......







推荐阅读
author-avatar
丽春院少爷
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有