Home >Java >javaTutorial >How to Add Color to Console Output Using System.out.println()?
How to Inject Color into Console Output with System.out.println()
Question:
How do I effortlessly infuse color into my console output to visually differentiate data?
Answer:
Embracing the power of ANSI escape codes, you can unleash a vibrant spectrum of colors in your console output. This technique, predominantly supported by Unix shell prompts, provides a convenient way to enhance data presentation. If you wish to enchant your terminals with this chromatic magic, simply follow these steps:
public static final String ANSI_RESET = "\u001B[0m"; public static final String ANSI_BLACK = "\u001B[30m"; public static final String ANSI_RED = "\u001B[31m"; public static final String ANSI_GREEN = "\u001B[32m"; public static final String ANSI_YELLOW = "\u001B[33m"; public static final String ANSI_BLUE = "\u001B[34m"; public static final String ANSI_PURPLE = "\u001B[35m"; public static final String ANSI_CYAN = "\u001B[36m"; public static final String ANSI_WHITE = "\u001B[37m";
System.out.println(ANSI_RED + "This text dances in fiery crimson!" + ANSI_RESET);
public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m"; public static final String ANSI_RED_BACKGROUND = "\u001B[41m"; public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m"; public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m"; System.out.println(ANSI_GREEN_BACKGROUND + "Emerald hues adorn this text's canvas!" + ANSI_RESET);
With this newfound control over console colors, you can effortlessly transform data into a visual masterpiece, adding depth and clarity to your programs' output.
The above is the detailed content of How to Add Color to Console Output Using System.out.println()?. For more information, please follow other related articles on the PHP Chinese website!