Java 将 ByteArrayInputStream 的内容转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24059266/
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-08-14 09:56:42  来源:igfitidea点击:

Convert Contents Of A ByteArrayInputStream To String

javastringbytearrayoutputstreambytearrayinputstream

提问by Mushy

I read this postbut I am not following. I have seen thisbut have not seen a proper example of converting a ByteArrayInputStreamto Stringusing a ByteArrayOutputStream.

我读了这篇文章,但我没有关注。我已经看到了这一点,但还没有看到将 a 转换ByteArrayInputStreamString使用 a的正确示例ByteArrayOutputStream

To retrieve the contents of a ByteArrayInputStreamas a String, is using a ByteArrayOutputstreamrecommended or is there a more preferable way?

要将 a 的内容检索ByteArrayInputStream为 a String,是使用ByteArrayOutputstream推荐的还是有更可取的方法?

I was considering this exampleand extend ByteArrayInputStreamand utilize a Decoratorto increase functionality at run time. Any interest in this being a better solution to employing a ByteArrayOutputStream?

我正在考虑这个例子并扩展ByteArrayInputStream和利用装饰器来增加运行时的功能。对这是使用 的更好解决方案有兴趣ByteArrayOutputStream吗?

采纳答案by Joop Eggen

A ByteArrayOutputStreamcan read from any InputStreamand at the end yield a byte[].

AByteArrayOutputStream可以从任何读取,InputStream最后产生 a byte[]

However with a ByteArrayInputStreamit is simpler:

然而,ByteArrayInputStream它更简单:

int n = in.available();
byte[] bytes = new byte[n];
in.read(bytes, 0, n);
String s = new String(bytes, StandardCharsets.UTF_8); // Or any encoding.

For a ByteArrayInputStreamavailable()yields the total number of bytes.

对于 aByteArrayInputStreamavailable()产生总字节数。



Answer to comment: using ByteArrayOutputStream

回答评论:使用 ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
for (;;) {
    int nread = in.read(buf, 0, buf.length);
    if (nread <= 0) {
        break;
    }
    baos.write(buf, 0, nread);
}
in.close();
baos.close();
byte[] bytes = baos.toByteArray();

Here in may be any InputStream.

这里可以是任何 InputStream。

回答by Mifmif

Use Scannerand pass to it's constructor the ByteArrayInputStreamthen read the data from your Scanner , check this example :

使用Scanner并传递给它的构造函数,ByteArrayInputStream然后从你的 Scanner 读取数据,检查这个例子:

ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(new byte[] { 65, 80 });
Scanner scanner = new Scanner(arrayInputStream);
scanner.useDelimiter("\Z");//To read all scanner content in one String
String data = "";
if (scanner.hasNext())
    data = scanner.next();
System.out.println(data);

回答by Bowser Jr.

Use Base64 encoding

使用 Base64 编码

Assuming you got your ByteArrayOutputStream :

假设你有你的 ByteArrayOutputStream :

ByteArrayOutputStream baos =...
String s = new String(Base64.Encoder.encode(baos.toByteArray()));

See http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html

请参阅http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html

回答by Cherry

Why nobody mentioned org.apache.commons.io.IOUtils?

为什么没人提org.apache.commons.io.IOUtils

import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;

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

Just one line of code.

只需一行代码。

回答by ZhekaKozlov

Java 9+ solution:

Java 9+ 解决方案:

new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);