java 从 InputStream 读取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1891606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:24:17  来源:igfitidea点击:

Read text from InputStream

java

提问by jthg

If I start with a java.io.InputStream, what's the easiest way to read the entire stream out into a String (assuming utf-8)?

如果我从 java.io.InputStream 开始,将整个流读入字符串(假设为 utf-8)的最简单方法是什么?

This should be pretty easy but I'm mostly a C# person and google is failing me on this. Thanks.

这应该很容易,但我主要是一个 C# 人,谷歌在这方面让我失望。谢谢。

采纳答案by zznate

Depending on what licenses you are comfortable with, it's a one linerwith Jakarta-Commons IO library.

根据您对哪种许可证感到满意,它是一个带有 Jakarta-Commons IO 库的单线。

回答by erickson

Dospecify the character encoding. Do notwaste code, introduce bugs, and slow execution with a BufferedReader.

指定字符编码。不要浪费代码、引入错误和使用BufferedReader.

Here is an example. You could parameterize it with a buffer size, encoding, etc.

这是一个例子。您可以使用缓冲区大小、编码等对其进行参数化。

static String readString(InputStream is) throws IOException {
  char[] buf = new char[2048];
  Reader r = new InputStreamReader(is, "UTF-8");
  StringBuilder s = new StringBuilder();
  while (true) {
    int n = r.read(buf);
    if (n < 0)
      break;
    s.append(buf, 0, n);
  }
  return s.toString();
}

回答by Peter Lawrey

Using Commons-IO is likely to be the best option. For your interest, another approach is to copy all the bytes and then convert it into a String.

使用 Commons-IO 可能是最好的选择。为了您的兴趣,另一种方法是复制所有字节,然后将其转换为字符串。

public static String readText(InputStream is, String charset) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[4096];
    for(int len;(len = is.read(bytes))>0;)
        baos.write(bytes, 0, len);
    return new String(baos.toByteArray(), charset);
}

回答by thug-gamer

I've found a nice way in Java 8 with streams:

我在 Java 8 中找到了一种使用流的好方法:

public static String readString(InputStream is) {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String content = br.lines().reduce("", String::concat);
    return content;
}

As stated above you can swap new InputStreamReader(is) with new InputStreamReader(is, "UTF-8"), but I have no experience with this constructor.

如上所述,您可以将 new InputStreamReader(is) 与 new InputStreamReader(is, "UTF-8") 交换,但我对这个构造函数没有经验。

回答by Dónal

Reading/writing from streams is remarkably painful in Java.

在 Java 中从流中读取/写入非常痛苦。

public static String getStreamContents(InputStream stream) throws IOException {

    StringBuilder content = new StringBuilder()

    Reader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"))
    String lineSeparator = System.getProperty("line.separator");

    try {
        String line
        while ((line = reader.readLine()) != null) {
            content.append(line + lineSeparator)
        }
        return content.toString()

    } finally {
        reader.close()
    }

}