为什么我的 ByteArrayInputStream 在从任何字符串创建后是空的?
why is my ByteArrayInputStream empty after creating it from any string?
基本上我使用(在 groovy
中)以下结构:
InputStream in = new ByteArrayInputStream("one two three four".getBytes());
但是当我打印它的内容时:
println(in.text)
我看到空行。为什么这样?以及如何将这些字节保存在我的 InputStream 中?
字节在InputStream
,你只是打印错了。尝试将 InputStream
转换为带有 Scanner
的 String
,然后打印它。
Scanner s = new Scanner(in).useDelimeter("\Z");
while (s.hasNext()) {
System.out.print(s.next());
}
您的原始代码在 Groovy 控制台中给我一个编译错误:
InputStream in = new ByteArrayInputStream("one two three four".getBytes());
println(in.text)
1 compilation error:
expecting EOF, found 'in' at line: 1, column: 13
in
是Groovy中的保留字。将其更改为 inn
一切正常:
InputStream inn = new ByteArrayInputStream("one two three four".getBytes())
println(inn.text)
有了这个输出:
one two three four
基本上我使用(在 groovy
中)以下结构:
InputStream in = new ByteArrayInputStream("one two three four".getBytes());
但是当我打印它的内容时:
println(in.text)
我看到空行。为什么这样?以及如何将这些字节保存在我的 InputStream 中?
字节在InputStream
,你只是打印错了。尝试将 InputStream
转换为带有 Scanner
的 String
,然后打印它。
Scanner s = new Scanner(in).useDelimeter("\Z");
while (s.hasNext()) {
System.out.print(s.next());
}
您的原始代码在 Groovy 控制台中给我一个编译错误:
InputStream in = new ByteArrayInputStream("one two three four".getBytes());
println(in.text)
1 compilation error:
expecting EOF, found 'in' at line: 1, column: 13
in
是Groovy中的保留字。将其更改为 inn
一切正常:
InputStream inn = new ByteArrayInputStream("one two three four".getBytes())
println(inn.text)
有了这个输出:
one two three four