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