0

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Wuzseen
  • 687
  • 3
  • 14
  • 20

1 Answers1

2

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

valodzka
  • 5,535
  • 4
  • 39
  • 50
  • +1 If you add an `out.reset()` call too, the buffer becomes ready for reuse. – Ravi K Thapliyal Aug 24 '13 at 20:31
  • Perhaps I spoke too soon, my calls to system.out are in one thread and the system.in are in a different thread. Not entirely sure where this would go, though I understand the idea. – Wuzseen Aug 24 '13 at 21:09