Charset forName() method in Java with Examples

The forName() method is a built-in method of the java.nio.charset returns a charset object for the named charset. In this function we pass a canonical name or an alias and its respective charset name is returned.

Syntax:

public static Charset forName?(String charsetName)

Parameters: The function accepts a single mandatory parameter charsetName which specifies the canonical name or the alias name whose object name is to be returned.



Return Value: The function returns a charset object for the named charset.

Errors and Exceptions: The function throws three exceptions as shown below:

  • IllegalCharsetNameException: It is thrown if the given charset name is illegal
  • IllegalArgumentException : It is thrown if the given charsetName is null
  • UnsupportedCharsetException : It is thrown if no support for the named charset is available in this instance of the Java virtual machine

Below is the implementation of the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Gets the charset
        Charset first = Charset.forName("ISO-2022-CN");
  
        // Prints the object
        System.out.println("The name for ISO-2022-CN is " + first);
    }
}

chevron_right


Output:

The name for ISO-2022-CN is ISO-2022-CN

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Gets the charset
        Charset first = Charset.forName("UTF16");
  
        // Prints the object
        System.out.println("The name for UTF16 is " + first);
    }
}

chevron_right


Output:

The name for UTF16 is UTF-16

Program 3

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            // Gets the charset
            Charset first = Charset.forName("");
  
            // Prints the object
            System.out.println("The name for null is " + first);
        }
        catch (Exception e) {
  
            // Prints the exception
            System.out.println("The exception is: " + e);
        }
    }
}

chevron_right


Output:

The exception is: java.nio.charset.IllegalCharsetNameException:

Program 4

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            // Gets the charset
            Charset first = Charset.forName("gopal");
  
            // Prints the object
            System.out.println("The name for gopal is " + first);
        }
        catch (Exception e) {
  
            // Prints the exception
            System.out.println("The exception is: " + e);
        }
    }
}

chevron_right


Output:

The exception is: java.nio.charset.UnsupportedCharsetException: gopal

Reference: https://docs.oracle.com/javase/10/docs/api/java/nio/charset/Charset.html#forName(java.lang.String)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.