Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top