当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Charset isSupported()用法及代码示例


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

用法

public final boolean isRegistered()

参数:该函数接受单个强制参数charsetName,该参数指定要检查的规范名称或别名。


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

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

  • IllegalCharsetNameException:如果给定的字符集名称不合法,则抛出该异常
  • IllegalArgumentException:如果给定的charsetName为null,则抛出该异常

下面是上述函数的实现:

示例1:

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

示例2:

// 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 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-



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Charset isSupported() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。