문제

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?

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"

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top