Open In App

Charset isSupported() method in Java with Examples

Last Updated : 01 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 isSupported()

Parameters: The function accepts a single mandatory parameter charset Name 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 charset Name is null

Below is the implementation of the above function:

Program 1:  

Java




// 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);
        }
    }
}


Output: 

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

 

Program 2: 

Java




// 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);
        }
    }
}


Output: 

ISO is supported or not? :false

 

Program 3: 

Java




// 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);
        }
    }
}


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-
 



Similar Reads

java.nio.charset.Charset Class in Java
In Java, Charset is a mapping technique used in Java to map the 16-bit Unicode sequence and sequences of bytes. It is also used to encode and decode the string data text into different character encoding. It comes under java.nio.charset.Charset package. The charset must begin with a number or letter. Every charset can decode and encode. For constru
2 min read
Instant isSupported() method in Java with Examples
In Instant class, there are two types of isSupported() method depending upon the parameters passed to it. isSupported(TemporalField field) isSupported() method of a Instant class used to Check if the specified field is supported by Instant class or not means using this method we can check if this instant can be queried for the specified field. The
2 min read
YearMonth isSupported(TemporalUnit) Method in Java with Examples
The isSupported(TemporalUnit) method of YearMonth class is used to Check if the specified TemporalUnit is supported by YearMonth class. Actually, this method checks if we can apply addition or subtraction operation using the passed unit to the specified unit this YearMonth. If false, then calling the plus(long, TemporalUnit) and minus methods will
2 min read
Year isSupported(TemporalUnit) Method in Java with Examples
The isSupported(TemporalUnit) method of Year class is used to Check if the specified TemporalUnit is supported by Year class. Actually, this method checks if we can apply addition or subtraction operation using the passed unit to the specified unit this year. If false, then calling the plus(long, TemporalUnit) and minus methods will throw an except
2 min read
ZoneOffset isSupported(TemporalField) method in Java with Examples
The isSupported(TemporalField) method of ZoneOffset Class in java.time package is used to check if the temporalField, passed as the parameter, is supported by the ZoneOffset or not. This method returns a boolean value stating the same. Syntax: public boolean isSupported(TemporalField temporalField) Parameters: This method accepts a parameter tempor
1 min read
LocalDate isSupported() method in Java with Examples
In LocalDate class, there are two types of isSupported() method depending upon the parameters passed to it. isSupported(TemporalField field) isSupported() method of a LocalDate class used to check if the specified field is supported by LocalDate class or not means using this method we can check if this LocalDate can be queried for the specified fie
2 min read
ZonedDateTime isSupported() method in Java with Examples
In ZonedDateTime class, there are two types of isSupported() method depending upon the parameters passed to it. isSupported(TemporalField field) isSupported() method of a ZonedDateTime class used to check if the specified field is supported by ZonedDateTime class or not means using this method we can check if this ZonedDateTime can be queried for t
2 min read
LocalTime isSupported() method in Java with Examples
In LocalTime class, there are two types of isSupported() method depending upon the parameters passed to it. isSupported(TemporalField field) isSupported() method of a LocalTime class used to Check if the specified field is supported by LocalTime class or not means using this method we can check if this LocalTime can be queried for the specified fie
2 min read
MonthDay isSupported() Method in Java with Examples
isSupported() method of the MonthDay class used to Check if the specified field is supported by MonthDay class or not means using this method we can check if this MonthDay can be queried for the specified field. The supported fields of ChronoField are: MONTH_OF_YEARYEAR All other ChronoField instances will return false. Syntax: public boolean isSup
2 min read
Year isSupported(TemporalField) Method in Java with Examples
The isSupported(TemporalField) method of Year class is used to check if the specified field is supported by Year class or not means using this method we can check if this Year object can be queried for the specified field. The supported fields of ChronoField are: YEAR_OF_ERAYEARERAAll other ChronoField instances will return false. Syntax: public bo
2 min read
Practice Tags :