📜  Java中的 Charset isSupported() 方法及示例

📅  最后修改于: 2022-05-13 01:54:49.064000             🧑  作者: Mango

Java中的 Charset isSupported() 方法及示例

isSupported()方法是Java.nio.charset的内置方法,用于检查是否支持给定的字符集。

语法

public final boolean isSupported()

参数:该函数接受一个强制参数字符集名称,它指定要检查的规范名称或别名。

返回值:函数返回一个布尔值。如果支持,则返回 true,否则返回 false。

错误和异常:该函数抛出两个异常,如下所示:

  • IllegalCharsetNameException :如果给定的字符集名称是非法的,则抛出它
  • IllegalArgumentException :如果给定的字符集名称为空,则抛出该异常

下面是上述函数的实现:

方案一:

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


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


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


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

方案二:

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);
        }
    }
}
输出:
ISO is supported or not? :false

方案 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);
        }
    }
}
输出:
Exception is java.nio.charset.IllegalCharsetNameException:

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