Defining a default character set in Java

I am programming in Java

I have code like:

byte[] b = test.getBytes(); 

The api states that if we do not specify a character encoding, it accepts the platform character encoding by default.

What is meant by "default character encoding"?

Does this mean Java coding or OS coding?

If this means OS encoding, how can I check the default encoding for Windows and Linux? In any case, can we get the default encoding using the command line?

+7
java character-encoding
source share
2 answers

This means the default encoding of the JVM you are working on,

To check the default encoding, you can do the following:

 System.getProperty("file.encoding"); 

which will return the default encoding (and the one used by getBytes () above).

+3
source share

The system property file.encoding is a specific JVM provider. In this particular case, it applies only to the Sun JVM, and it cannot work with JVMs from vendors other than Sun.

Rather, use the Java SE API provided by Charset#defaultCharset() .

 Charset defaultCharset = Charset.defaultCharset(); 
+29
source share

All Articles