Charset isSupported() method in Java with Examples

The isSupported() method is a built-in method of the java.nio.charset checks if a given charset is supported or not.

Syntax:

public final boolean isRegistered()

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



Return Value: The function returns a boolean value. It returns true if it is supported, else it returns false.

Errors and Exceptions: The function throws two 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

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;
  
public class GFG {
  
    public static void main(String[] args)
    {
        try {
            System.out.println("ISO-2022-CN"
                               + " is supported or not? :"
                               + Charset.isSupported("ISO-2022-CN"));
        }
        catch (Exception e) {
            System.out.println("Exception: "
                               + e);
        }
    }
}

chevron_right


Output:

ISO-2022-CN is supported or not? :true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
  
public class GFG {
  
    public static void main(String[] args)
    {
        try {
            System.out.println("ISO is "
                               + "supported or not? :"
                               + Charset.isSupported("ISO"));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

ISO is supported or not? :false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above function
import java.nio.charset.Charset;
  
public class GFG {
  
    public static void main(String[] args)
    {
        try {
            System.out.println("NULL is "
                               + "supported or not? :"
                               + Charset.isSupported(""));
        }
        catch (Exception e) {
            System.out.println("Exception: "
                               + e);
        }
    }
}

chevron_right


Output:

Exception is java.nio.charset.IllegalCharsetNameException:

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/charset/Charset.html#isSupported-java.lang.String-

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.