Java 为什么我的ByteArrayInputStream在从任何字符串创建后为空?

Java 为什么我的ByteArrayInputStream在从任何字符串创建后为空?,java,groovy,inputstream,Java,Groovy,Inputstream,基本上,我正在使用(在groovy中)以下构造: InputStream in = new ByteArrayInputStream("one two three four".getBytes()); 但当我打印它的内容时: println(in.text) 我看到空行。为什么会这样?我如何将这些字节保存在我的InputStream中?这些字节在InputStream中,您只是打印错误而已。尝试使用扫描仪将输入流转换为字符串,然后打印它 Scanner s = new Scanner(in)

基本上,我正在使用(在
groovy
中)以下构造:

InputStream in = new ByteArrayInputStream("one two three four".getBytes());
但当我打印它的内容时:

println(in.text)

我看到空行。为什么会这样?我如何将这些字节保存在我的InputStream中?

这些字节在
InputStream中,您只是打印错误而已。尝试使用
扫描仪将
输入流
转换为
字符串
,然后打印它

Scanner s = new Scanner(in).useDelimeter("\\Z");
while (s.hasNext()) {
    System.out.print(s.next());
}

字节在
输入流中,您只是打印错误。尝试使用
扫描仪将
输入流
转换为
字符串
,然后打印它

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
中的
是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)

1 compilation error:

expecting EOF, found 'in' at line: 1, column: 13
中的
是Groovy中的保留字。将其更改为
inn
所有操作都正常:

InputStream inn = new ByteArrayInputStream("one two three four".getBytes())
println(inn.text)
使用此输出:

one two three four