为什么这段代码不起作用,除非我取消注释该System.out.print(" ");
行?
3个案例:
System.out.print(" ");
结果后outprint.write(var);
:h e l l o w o r l
System.out.print(" ");
结果之前outprint.write(var);
:h e l l o w o r l d
- 不
System.out.print(" ");
显示任何内容
总而言之,我确实将System.out
实例(它是 a PrintStream
)传递给对象的out
属性FilterOutputStream
(它需要 a OutputStream
)。
import java.io.FilterOutputStream;
import java.io.IOException;
public class CodeCesar {
public static void main(String[] args) {
FilterOutputStream outputstream = new FilterOutputStream(System.out);
String line = "hello world";
char[] lst_char = line.toCharArray();
for (char var : lst_char) {
try {
outputstream.write(var);
System.out.print(" "); <--------- THIS LINE
} catch (IOException e) {
e.printStackTrace();
}
}
}
}