Java Console charset() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 17+

Definition

In Java, console.charset() returns the java.nio.charset.Charset the console uses to encode the text you print and decode the keys the user types. It describes the terminal itself (keyboard and display), as set up by the operating system or the user.

It matters because the console charset is not always the same as Charset.defaultCharset(). On Linux and macOS both are usually UTF-8. On Windows, Command Prompt often still uses an older code page such as 437, while Java 18 and later use UTF-8 as the default charset for files.

An instance method of the java.io.Console class, available since Java 17. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.charset(...).

Requires Java 17 or later.

charset() was added in Java 17, so the examples on this page do not compile on Java 8 to 16. There, Charset.defaultCharset() or System.getProperty("file.encoding") is the closest substitute, but on Windows they usually describe the file encoding, not the console code page. Java 19+ also sets the stdout.encoding property, which normally matches the console when output goes to a terminal.

Syntax

Call it on the Console object; it takes no arguments:

Java
Console console = System.console();
if (console != null) {
    Charset cs = console.charset();   // e.g. UTF-8
    String name = cs.name();          // "UTF-8"
}
CallWhat it does
console.charset()Returns the Charset the terminal uses.
console.charset().name()Canonical name, such as UTF-8 or IBM437.
console.charset().newEncoder().canEncode(s)Checks whether the terminal charset can represent the text s.
Charset.defaultCharset()The JVM default charset, which may differ from the console charset.

Added in Java 17; the Console class itself has existed since Java 6. The value comes from the host environment, so the same program can print different names on different computers.

Library declarations
Java
// java.io.Console (Java 17+)
public Charset charset()

Parameters

ParameterDescription
Nonecharset() takes no parameters.

Return Value

Return ValueDescription
CharsetThe charset used for console input and output. It may not be the same as Charset.defaultCharset().

Compare with Similar Methods

Ways to find out about text encoding:

MethodHow it differs
Charset.defaultCharset()JVM default for files when no charset is given; UTF-8 by default since Java 18.
System.getProperty("stdout.encoding")Java 19+ property: the encoding used by System.out.
System.getProperty("native.encoding")Java 17+ property: the operating system's own default encoding.
writer()The PrintWriter that encodes output with the console charset.

Java Examples

Example 1 — Compare the Console Charset with the Default Charset

Print both charsets and check whether they are equal. The answer depends on the operating system and terminal settings.

Java
import java.io.Console;
import java.nio.charset.Charset;

public class ConsoleCharsetShow {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        Charset cs = console.charset();
        Charset def = Charset.defaultCharset();
        console.printf("Console charset: %s%n", cs.name());
        console.printf("Default charset: %s%n", def.name());
        console.printf("Same as default? %b%n", cs.equals(def));
    }
}

How It Works

  1. Two charsets. console.charset() asks the terminal, while Charset.defaultCharset() asks the JVM for its file default.
  2. Canonical names. name() prints the official name of each charset, so code page 437 shows up as IBM437.
  3. UTF-8 terminal. On a typical Linux or macOS terminal with Java 18+, both are UTF-8 and equals returns true.
  4. Windows mismatch. Command Prompt with code page 437 reports IBM437 for the console, while the JVM default is UTF-8, so the check prints false.

Example 2 — Check Which Characters the Terminal Can Show

A CharsetEncoder from the console charset reports whether a character can be represented. The source code uses \u escapes, so it stays plain ASCII.

Java
import java.io.Console;
import java.nio.charset.CharsetEncoder;

public class ConsoleCharsetCanEncode {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        CharsetEncoder encoder = console.charset().newEncoder();
        String[] names = {"letter A", "e with acute", "check mark", "euro sign"};
        String[] samples = {"A", "\u00e9", "\u2713", "\u20ac"};
        console.printf("Terminal charset: %s%n", console.charset().name());
        for (int i = 0; i < names.length; i++) {
            String answer = encoder.canEncode(samples[i]) ? "yes" : "no";
            console.printf("%-13s %s%n", names[i], answer);
        }
    }
}

How It Works

  1. An encoder for the terminal. newEncoder() creates a CharsetEncoder that knows exactly which characters the console charset can turn into bytes.
  2. Escapes keep the file ASCII. "\u00e9", "\u2713", and "\u20ac" are the e-acute, check mark, and euro sign, written with Unicode escapes instead of the symbols themselves.
  3. UTF-8 covers everything. UTF-8 can encode every Unicode character, so all four answers are yes.
  4. Code page 437 is limited. It contains A and e-acute but has no check mark and no euro sign, so those two answers are no. Printing them anyway would show replacement characters.

Example 3 — Print charset() Next to the Encoding System Properties

Compare the console charset with the system properties you might use on older Java versions. getProperty with a default avoids printing null for missing properties.

Java
import java.io.Console;

public class ConsoleCharsetProperties {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        console.printf("console.charset(): %s%n", console.charset().name());
        console.printf("stdout.encoding:   %s%n", System.getProperty("stdout.encoding", "(not set)"));
        console.printf("native.encoding:   %s%n", System.getProperty("native.encoding", "(not set)"));
        console.printf("file.encoding:     %s%n", System.getProperty("file.encoding", "(not set)"));
    }
}

How It Works

  1. The Java 17 answer. The first line asks the console directly, which is the reliable source on Java 17 and later.
  2. Properties with a fallback. System.getProperty(key, "(not set)") returns the second argument when a property does not exist, for example stdout.encoding on Java versions before 19.
  3. Three different meanings. stdout.encoding is what System.out uses, native.encoding is the operating system default, and file.encoding is the JVM default for files.
  4. All agree here. In a UTF-8 terminal on Linux or macOS with a recent JDK every value is UTF-8; on Windows the console and stdout.encoding lines often show a code page instead.

Frequently Asked Questions

The Charset the console uses to read typed text and write printed text, for example UTF-8 on most Linux and macOS terminals or IBM437 in a Windows Command Prompt using code page 437.
Java 17. The Console class itself has existed since Java 6.
The default charset is used for files and is UTF-8 by default since Java 18. The console charset follows the terminal, and Windows terminals often still use a legacy code page such as 437.
There is no charset() method before Java 17. Charset.defaultCharset() and System.getProperty("file.encoding") are common guesses, but on Windows they usually report the file encoding rather than the console code page.
Not through Console, because charset() only reports the encoding. Change the terminal instead, for example with chcp 65001 in Windows Command Prompt before starting the program.

Next: flush()

Force any buffered console output to appear on the screen right away.

flush() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful