How to print color in console using System.out.println?

后端 未结 13 2318
灰色年华
灰色年华 2020-11-22 05:22

How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.

13条回答
  •  名媛妹妹
    2020-11-22 06:08

    You could do this using ANSI escape sequences. I've actually put together this class in Java for anyone that would like a simple workaround for this. It allows for more than just color codes.

    https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a

    (Ported from: https://github.com/nathan-fiscaletti/ansi-util)

    Example Use:

    StringBuilder sb = new StringBuilder();
    
    System.out.println(
        sb.raw("Hello, ")
          .underline("John Doe")
          .resetUnderline()
          .raw(". ")
          .raw("This is ")
          .color16(StringBuilder.Color16.FG_RED, "red")
          .raw(".")
    );
    

提交回复
热议问题