Default Character encoding in Javaor charset is the character encoding used by JVM to convert bytes into Strings or characters when youdon't define java system property "file.encoding". Java gets character encoding by calling System.getProperty("file.encoding","UTF-8") at the time of JVM start-up. So if Java doesn't get any file.encoding attribute it uses "UTF-8" character encoding for all practical purpose e.g. on String.getBytes() or Charset.defaultCharSet(). Most important pointto remember is that Java caches character encoding or value of system property "file.encoding" in most of its core classes like InputStreamReader which needs character encoding after JVM started. so if you change system property "file.encoding" programmatically you don't see desired effect and that's why you should always work with your own character encoding provided to your application and if its need to be set than set character encoding or charset while you start JVM.In this Java tutorial, we will see couple of different way by which we can set default character encoding or charset of Java and how toretrieve value of charset inside java program.
For those who are not very familiar with character encoding or char-setin Javahere is a layman's introduction "since every data in computer is represented in bytes and Strings are essentially collection of characters, so to convert bytes into character JVM needs to knowwhich combination of byte represent which character and this is what character encoding tells JVM. Since there are many languagesin world other than English like Hindi, Mandarin, Japanese Kanji etc and so many characters, same combination of bytes can represent different characters in different character encoding andthat's why using correct character encoding is must while converting bytes into String in Java".
How to get Default character encoding in Java ?
There are multiple ways to get default character encoding in Java like by using system property “file.encoding” or by using java.nio.CharSet class. You can choose whatever suits your need. Let’s see them in detail.
1) "file.encoding" system property
The easiest way to get default character encoding in Java is to call System.getProperty("file.encoding"), which will returndefault character encoding if JVM started with -Dfile.encoding property or program has not called System.setProperty("file.encoding, encoding). in the later case, it may just give the value of that system property while various
java.nio.Charset provides a convenient static method Charset.defaultCharset() which returns default character encoding in Java. Check the example of getting default char encoding in java using Charset in the code section.
3) by using Code InputStreamReader.getEncoding()
This is kind of shortcut where you use default constructor of InputStreamReader and then later gets which character encoding ithas used by calling reader.getEncoding() . See the code example of how to get default character encoding using InputStreamReader.getEncoding() method in code section.
How to set Default character encoding in Java ?
Just like different ways of getting default character encoding or charset in Java there are many ways to set default charset in Java. Here are some of the way:
1. Using System property "file.encoding"
by providing the file.encoding system property when JVM starts e.g. java -Dfile.encoding="UTF-8" HelloWorld.
2. Using Environment variable "JAVA_TOOLS_OPTIONS"
If by anyway you don't have control how JVM starts up may be JVM is starting through some scripts which doesn't provide anyway to accept system properties. you can set environment variable JAVA_TOOL_OPTIONS to -Dfile.encoding="UTF-16" or any othercharacter encoding and it will picked up any JVM starts in your windows machine. JVM will also print "Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16" on console to indicate that it has picked JAVA_TOOS_OPTIONS. here is example of setting default characterencoding using JAVA_TOOLS_OPTIONS
test@system:~/java java HelloWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16
defaultCharacterEncoding by property after updating file.encoding : UTF-16
defaultCharacterEncoding by code after updating file.encoding : UTF8
defaultCharacterEncoding by java.nio.Charset after updating file.encoding : UTF-8
Important points to note:
1) JVM caches value of default character encoding once JVM starts and so is the case for default constructors of InputStreamReader and other core Java classes. So calling System.setProperty("file.encoding" , "UTF-16") may not have desire effect.
2) Always work with your own character encoding if you can, that is more accurate and precise way of converting bytes to Strings.
That’s all on how to get default character encoding in Java and how to set it. This becomes more important when you are
writing international application which supports multiple languages. I indeed come across character encoding issues while
writing reports in kanji (Japanese) language which I plan to share in another post, but good knowledge of Character
encoding like UTF-8, UTF-16 or ISO-8859-5 and how Java supports Character Encoding in