John Threepwood
John Threepwood

Reputation: 16143

What encoding/character set does Java use per default for BufferedReader?

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?
}

Upvotes: 2

Views: 4065

Answers (2)

Stephen C
Stephen C

Reputation: 718906

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.

Upvotes: 5

xingbin
xingbin

Reputation: 28279

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());
}

Upvotes: 4

Related Questions