How to Get and Set Default Character Encoding or Charset in Java?


In Java, the default character encoding is determined by the 'file.encoding' which is a system property and is usually set by the operating system or the JVM. However, sometimes a Java programmer may need to get or set the default character encoding programmatically for various reasons. For this purpose, the 'java.nio.charset' package provides various classes and methods. In this article, we are going to learn the different ways to get and set default character encoding or charset.

Get and Set Default Character Encoding or Charset in Java

First, let's discuss what is default character encoding or charset.

Default Character Encoding

Character encoding or charset is a way of mapping a set of characters to a sequence of bytes. But, why do we need to map, here is the answer. We know that the data in a computer system is represented in the form of bytes but we interact with computers using a sequence of characters called String. Therefore, we need to map byte into characters so that the JVM can recognize which set of bytes represent which character. Furthermore, Different character encodings may use different numbers of bytes to represent the same character or may support different sets of characters altogether. For example, ASCII is a 7-bit encoding that can represent 128 characters, while UTF-8 is a variable-length encoding that can represent over a million characters.

To Get Default Character Encoding or Charset

We are going to use the following ways in our example program to find the default character encoding or charset:

  • file.encoding: It is an attribute of 'System.getProperty()' method that returns the value of charset.

  • defaultCharset() method: It is a method of 'java.nio.charset' package that is used for retrieving the default character encoding of Java Virtual Machine.

  • getEncoding() method: It is a method of the 'java.io.FileReader' class that is used for retrieving the default character encoding of streams.

Example 1

In the following example, we will demonstrate the use of 'file.encoding' attribute and 'defaultCharset()' method in getting default character encoding.

import java.nio.charset.Charset;
public class GetDefaultCharset {
   public static void main(String[] args) {
      // to get the default charset
      Charset defaultCharset1 = Charset.defaultCharset();
      String defaultCharset2 = System.getProperty("file.encoding");
      // printing the name of default charset
      System.out.println("Default charset1: " + defaultCharset1.name());
      System.out.println("Default charset2: " + defaultCharset2);
   }
}

Output

Default charset1: UTF-8
Default charset2: UTF-8

Example 2

In this example, we will create a byte array of characters and pass them as a stream to check its current character encoding using the 'getEncoding()' method.

import java.io.*;
import java.nio.charset.Charset;
public class GetDefaultCharset {
   public static void main(String args[]) {
      // creating an array of byte
      byte[] chAray = {'T','U','T','O','R','I','A','L'};
      // converting the array into stream 
      InputStream inStrm = new ByteArrayInputStream(chAray);
      // reading the stream
      InputStreamReader strmRdr = new InputStreamReader(inStrm);
      // checking and printing the charset 
      String defaultCharset = strmRdr.getEncoding();
      System.out.println("Default charset: " + defaultCharset);   
   }
}

Output

Default charset: UTF8

To Set Default Character Encoding or Charset

We can use the 'System.setProperty()' by passing the 'file.encoding' attribute along with a type of Charset to set the default character encoding.

Example 3

The following example illustrates how to set the default character encoding in Java.

import java.nio.charset.Charset;
public class SetDefaultCharset {
   public static void main(String[] args) {
      // setting the default charset to ISO-8859-1
      System.setProperty("file.encoding", "ISO-8859-1");
      // to get the new default charset
      Charset defaultCharset = Charset.defaultCharset();
      // printing the name and aliases of default charset
      System.out.println("Default charset: " + defaultCharset.name());
      System.out.println("Aliases: " + defaultCharset.aliases());
   }
}

Output

Default charset: UTF-8
Aliases: [unicode-1-1-utf-8, UTF8]

Conclusion

In this article, we have learned various ways to get and set default character encoding in Java with the help of example programs. First, we understood the basics of default Charset and then, discussed three Java programs that show the practical implementation of discussed ways of getting and setting default Charset.

Updated on: 20-Jul-2023

699 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements