Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining default character set of platform in Java

I am programming in Java

I have the code as:

byte[] b = test.getBytes();

In the api it is specified that if we do not specify character encoding it takes the default platform character encoding.

What is meant by "default platform character encoding" ?

Does it mean the Java encoding or the OS encoding ?

If it means OS encoding the how can i check the default character encoding of Windows and Linux ? Is there anyway we can get the default character encoding using command line ?

like image 468
Anand Sunderraman Avatar asked Apr 20 '10 17:04

Anand Sunderraman


People also ask

What is the default character set in Java?

encoding attribute, Java uses “UTF-8” character encoding by default. Character encoding basically interprets a sequence of bytes into a string of specific characters.

Which method is used for using default character encoding of the OS in Java?

defaultCharset() which returns default character encoding in Java.

What is character set in Java?

A character set is a set of textual and graphic symbols, each of which is mapped to a set of nonnegative integers. The first character set used in computing was US-ASCII.


1 Answers

The system property file.encoding is JVM vendor specific. In this specific case it's only applicable on the Sun JVM and it may not work on JVM's from other vendors than Sun.

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

Charset defaultCharset = Charset.defaultCharset();
like image 85
BalusC Avatar answered Oct 23 '22 16:10

BalusC