繁体   English   中英

我想将“标准”output stream(System.out)链接到 PrintWriter stream。 但是我不能。 为什么?

[英]I want to chain the“standard” output stream (System.out) to PrintWriter stream. But I can not. Why?

我想更换:

System.out.println("It works properly");

经过:

PrintWriter myPrintWriter = new PrintWriter(System.out);
myPrintWriter.print("This text is not displayed on my screen");

不幸的是,第二个选项不起作用,我不知道为什么。 我正在从头开始学习 Java,我正在尝试理解一些基本问题、概念,所以......请帮忙。 对不起我的英语;)

stream 不会自动刷新。 使用myPrintWriter.flush()在控制台上获取结果。

演示:

import java.io.PrintWriter;

class Main {
    public static void main(String[] args) {
        PrintWriter myPrintWriter = new PrintWriter(System.out);
        myPrintWriter.print("This text is not displayed on my screen");
        myPrintWriter.flush();
    }
}

Output:

This text is not displayed on my screen

暂无
暂无

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

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