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
Convert Contents Of A ByteArrayInputStream To String
提问by Mushy
I read this postbut I am not following. I have seen thisbut have not seen a proper example of converting a ByteArrayInputStream
to String
using a ByteArrayOutputStream
.
我读了这篇文章,但我没有关注。我已经看到了这一点,但还没有看到将 a 转换ByteArrayInputStream
为String
使用 a的正确示例ByteArrayOutputStream
。
To retrieve the contents of a ByteArrayInputStream
as a String
, is using a ByteArrayOutputstream
recommended or is there a more preferable way?
要将 a 的内容检索ByteArrayInputStream
为 a String
,是使用ByteArrayOutputstream
推荐的还是有更可取的方法?
I was considering this exampleand extend ByteArrayInputStream
and 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 ByteArrayOutputStream
can read from any InputStream
and at the end yield a byte[]
.
AByteArrayOutputStream
可以从任何读取,InputStream
最后产生 a byte[]
。
However with a ByteArrayInputStream
it 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 ByteArrayInputStream
available()
yields the total number of bytes.
对于 aByteArrayInputStream
available()
产生总字节数。
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 Scanner
and pass to it's constructor the ByteArrayInputStream
then 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);