简体   繁体   English

分离System.Out消息和扫描程序(Java)

[英]Separating System.Out messages and Scanner (Java)

Is there a way, without using a gui package of any sort, to not have System.out inject itself into the middle of your prompt? 有没有办法,没有使用任何类型的gui包,没有System.out注入到你的提示中间?

The scenario is a threaded program where you send and receive messages to a server. 该场景是一个线程程序,您可以在其中向服务器发送和接收消息。

While writing to the server, you get a message from the server. 写入服务器时,您会收到来自服务器的消息。

"I'm writinMESSAGE FROM SERVERg a message" “我正在写一封来自SERVERg的消息”

So you basically end up seeing something like that in your console. 所以你基本上最终会在你的控制台中看到类似的东西。

Is there a way to circumvent this problem and essentially separate the prompt from the input? 有没有办法绕过这个问题,并基本上将提示与输入分开?

You can do something like this to buffer output temporary: 您可以执行以下操作来缓冲输出临时:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream oldOut = System.out;
    System.setOut(new PrintStream(out));

    // System.in read code

    System.setOut(oldOut);
    System.out.println(out.toString());

Same for System.err System.err

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

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