There is a task: using the System.out.println and System.err.println commands to display text in a red frame. Like this:

******* *текст* ******* 

But when outputting to IDEA all the time, the text flies to the frame, then after, then differently, but not on the instructions. Eclipse has seen what works. I understand that there is a problem in the streams, but it does not work out.

 System.err.print("*******\n"); System.err.print("|"); System.out.print("text"); System.err.print("|"); System.err.print("*******\n"); 
  • 2
    Do not use stderr for the red light. It is not buffered and can jump anywhere. - 0xdb
  • Add: in addition, the red color only works in the IDE. In a real console, the text will remain exactly the same. - Pavel Mayorov

3 answers 3

Try this (clear the buffer after each output):

 print(System.err, "*******\n"); print(System.err,"|"); print(System.out,"text"); print(System.err,"|"); print(System.err,"*******\n"); private void print(PrintStream ps, String output) { ps.print(output); ps.flush(); } 

    The fact is that System.out.print buffers data for output, unlike System.err.print .

    What does it mean ?

    For example, you type using System.out.println . Then perform some operations, then print again. System.out.println will print all the elements at once, rather than one at a time.

    System.err.print instantly prints the elements without buffering the data.

    • Here would be to throw a link to the documentation or some kind of normal link. I’m not really sure that System.err practicing unbuffered output, although there’s certainly a logic in this - Barmaley

    Solved the problem like this:

     public static void main(String[] args) { try { System.err.print("**********\n| "); System.err.flush(); Thread.sleep(50); System.out.print("text"); System.out.flush(); Thread.sleep(50); System.err.print(" |\n**********"); } catch (InterruptedException e) { e.printStackTrace(); } }