2

What encoding/character set does Java use per default when we create a new BufferedReader object without providing an encoding explicitly?

For example:

try (final BufferedReader reader = new BufferedReader(new FileReader("my_file.txt"))) {
  reader.readLine(); // What encoding is used to read the file?
}
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
  • If you want to know _how_ the default is set, dupe https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding https://stackoverflow.com/questions/1006276/what-is-the-default-encoding-of-the-jvm https://stackoverflow.com/questions/1749064/how-to-find-the-default-charset-encoding-in-java https://stackoverflow.com/questions/2677419/determining-default-character-set-of-platform-in-java and more – dave_thompson_085 Sep 01 '18 at 15:41

2 Answers2

5

BufferedReader doesn't do any decoding. It is a wrapper for another Reader ... which may or may not do decoding.

FileReader decodes using the JVM's default character encoding, as returned by Charset.defaultCharset()

The javadoc states:

Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
4

FileReader is an InputStreamReader which uses FileInputStream as input, and an InputStreamReader uses the default charset when constructed without specified charset.

In the source code jdk10, it use Charset.defaultCharset():

public static StreamDecoder forInputStreamReader(InputStream in,
                                                 Object lock,
                                                 String charsetName)
    throws UnsupportedEncodingException
{
    String csn = charsetName;
    if (csn == null)
        csn = Charset.defaultCharset().name(); // get default charset
    try {
        if (Charset.isSupported(csn))
            return new StreamDecoder(in, lock, Charset.forName(csn));
    } catch (IllegalCharsetNameException x) { }
    throw new UnsupportedEncodingException (csn);
}

which

Returns the default charset of this Java virtual machine.

The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.

You can print it:

public static void main(String[] args) {
    System.out.println(Charset.defaultCharset());
}
xingbin
  • 27,410
  • 9
  • 53
  • 103