Change font colors in Java console output


Introduction

The most basic output of most software is text in a console. That's how most programmers start, a simple "Hello World!" printed in the console. Although not very obvious, it is possible to customize some of the color output and add some style to your program.

To change terminal colors, you just need to add an ANSI code before your string.

One of the most practical way to do that is to create a class that can store static strings with that code, that can be easily imported to your projects. Your strings can have obvious names for better code readability.

Let's have a look.

Usage

Simply append to the beginning of the string the ANSI color code you want to use.

System.out.println("\u001B[30m" + "Hello World in red!");

Using a Java class

You can search the internet and use the ANSI codes in any way you want. Personally, I like having a class to store static strings to hold the code. It's very easy to implement:

public class ConsoleColors {

  public static final String TEXT_RESET  = "\u001B[0m";

  public static final String TEXT_BLACK  = "\u001B[30m";
  public static final String TEXT_RED    = "\u001B[31m";
  public static final String TEXT_GREEN  = "\u001B[32m";
  public static final String TEXT_YELLOW = "\u001B[33m";
  public static final String TEXT_BLUE   = "\u001B[34m";
  public static final String TEXT_PURPLE = "\u001B[35m";
  public static final String TEXT_CYAN   = "\u001B[36m";
  public static final String TEXT_WHITE  = "\u001B[37m";

  public static final String TEXT_BRIGHT_BLACK  = "\u001B[90m";
  public static final String TEXT_BRIGHT_RED    = "\u001B[91m";
  public static final String TEXT_BRIGHT_GREEN  = "\u001B[92m";
  public static final String TEXT_BRIGHT_YELLOW = "\u001B[93m";
  public static final String TEXT_BRIGHT_BLUE   = "\u001B[94m";
  public static final String TEXT_BRIGHT_PURPLE = "\u001B[95m";
  public static final String TEXT_BRIGHT_CYAN   = "\u001B[96m";
  public static final String TEXT_BRIGHT_WHITE  = "\u001B[97m";

  public static final String TEXT_BG_BLACK  = "\u001B[40m";
  public static final String TEXT_BG_RED    = "\u001B[41m";
  public static final String TEXT_BG_GREEN  = "\u001B[42m";
  public static final String TEXT_BG_YELLOW = "\u001B[43m";
  public static final String TEXT_BG_BLUE   = "\u001B[44m";
  public static final String TEXT_BG_PURPLE = "\u001B[45m";
  public static final String TEXT_BG_CYAN   = "\u001B[46m";
  public static final String TEXT_BG_WHITE  = "\u001B[47m";

  public static final String TEXT_BRIGHT_BG_BLACK  = "\u001B[100m";
  public static final String TEXT_BRIGHT_BG_RED    = "\u001B[101m";
  public static final String TEXT_BRIGHT_BG_GREEN  = "\u001B[102m";
  public static final String TEXT_BRIGHT_BG_YELLOW = "\u001B[103m";
  public static final String TEXT_BRIGHT_BG_BLUE   = "\u001B[104m";
  public static final String TEXT_BRIGHT_BG_PURPLE = "\u001B[105m";
  public static final String TEXT_BRIGHT_BG_CYAN   = "\u001B[106m";
  public static final String TEXT_BRIGHT_BG_WHITE  = "\u001B[107m";
}

You can go even further and create some helping methods to output colorful texts in the console. In the following example there are three methods, one to build a string with the AINSI color codes, another that will print a line using the AINSI color codes, and another that will check if the color string parameter is a valid AINSI code.

Note that the methods bellow should be in the same class above. In addition, I'm also using an array called ALL_COLORS, that contains all the color strings, to make validation easier.

  public static boolean isValidColor(String color){
      for (String checker : ALL_COLORS) {
          if (color.equals(checker)) return true;
      }
      return false;
  }

  public static void printColoredLine(String bgColor, String textColor, String toPrint) {
      System.out.println(buildColoredString(bgColor, textColor, toPrint));
  }

  public static String buildColoredString(String bgColor, String textColor, String str){
      String toReturn = "";
      if (bgColor != null && isValidColor(bgColor)) toReturn += bgColor;
      if (textColor != null && isValidColor(textColor)) toReturn += textColor;
      return toReturn + str + TEXT_RESET;
  }

It's important to keep in mind that once the color is changed, it will remain as that until your program finishes or you change back to the default or new color.

For that reason, it's advised to append to the end of your string the ANSI code to return to the default color.

If you want to save even more time, download a copy of this class in here.

Have fun!