简体   繁体   English

Java中的printWriter

[英]printWriter in Java

I have a question regarding to printWriter, please see code below: 我对printWriter有疑问,请参见下面的代码:

PrintWriter out = new PrintWriter(System.out);
Scanner n = new Scanner(System.in);

while (n.hasNext()) {
    String str = n.next();
    if (str.equals("m")) {
        break;
    } else {
        out.printf("%s", str);
        out.print(" ");
    }   
}
n.close();
// out.close();
System.out.println("input is successful");

When I input something by keyboard, it output on screen what i input, but it did not process the rest of the program, which meant it did not print "input is successful", however, when I delete the command of out.close(); 当我用键盘输入内容时,它在屏幕上输出我输入的内容,但是它没有处理程序的其余部分,这意味着它没有打印“输入成功”,但是,当我删除out.close( ); it kept running the rest program, so I want to ask what does out.close() mean? 它一直在运行其余程序,所以我想问out.close()是什么意思? I thought it just means to stop writing for output, I guess it has something to do with the parameter I put in the new PrintWriter(...) 我认为这只是意味着停止为输出而写,我想这与我在新PrintWriter(...)中放入的参数有关

PrintWriter.close() will also close the underlying stream, ie System.out . PrintWriter.close()也将关闭基础流,即System.out This effectively means that any subsequent outputs to System.out will be ignored. 这实际上意味着将忽略System.out任何后续输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM