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

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

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

newDecoder()方法是Java.nio.charset的内置方法,为此 charset 构造了一个新的解码器。
.

语法

public abstract CharsetDecoder newDecoder()

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

返回值:该函数为此字符集返回一个新的解码器

下面是上述函数的实现:

方案一:

// Java program to demonstrate
// the above function
  
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creates charset
        Charset Charset1 = Charset.forName("UTF8");
  
        // Creates Decoder
        CharsetDecoder Decoder = Charset1.newDecoder();
  
        // Prints decoder
        System.out.println(Decoder);
    }
}
输出:
sun.nio.cs.UTF_8$Decoder@232204a1

方案二:

// Java program to demonstrate
// the above function
  
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creates charset
        Charset Charset1 = Charset.forName("UTF-16");
  
        // Creates Decoder
        CharsetDecoder Decoder = Charset1.newDecoder();
  
        // Prints decoder
        System.out.println(Decoder);
    }
}
输出:
sun.nio.cs.UTF_16$Decoder@232204a1

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/charset/Charset.html#newDecoder–