當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。