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


Java CharsetEncoder charset()用法及代码示例


charset()方法是java.nio.charset.CharsetEncoder的内置方法,返回创建此编码器的字符集。

用法

public final Charset charset()

参数:该函数不接受任何参数。


返回值:该函数返回此编码器的字符集。

下面是上述函数的实现:

示例1:

// Java program to implement 
// the above function 
import java.nio.CharBuffer; 
import java.nio.charset.Charset; 
import java.nio.charset.CharsetEncoder; 
  
public class Main { 
    public static void main(String[] args) throws Exception 
    { 
        // Gets the encoder 
        CharsetEncoder encoder = Charset.forName("UTF16").newEncoder(); 
  
        // Prints CodingErrorAction 
        System.out.println(encoder.charset()); 
    } 
}
输出:
UTF-16

示例2:

// Java program to implement 
// the above function 
import java.nio.CharBuffer; 
import java.nio.charset.Charset; 
import java.nio.charset.CharsetEncoder; 
  
public class Main { 
    public static void main(String[] args) throws Exception 
    { 
        // Gets the encoder 
        CharsetEncoder encoder = Charset.forName("UTF8").newEncoder(); 
  
        // Prints CodingErrorAction 
        System.out.println(encoder.charset()); 
    } 
}
输出:
UTF-8

参考: https://docs.oracle.com/javase/10/docs/api/java/nio/charset/CharsetEncoder.html#charset()



相关用法


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