📌  相关文章
📜  Java中的 CharsetDecoder averageCharsPerByte() 方法及示例

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

Java中的 CharsetDecoder averageCharsPerByte() 方法及示例

averageCharsPerByte()方法是Java.nio.charset.CharsetDecoder 类的内置方法,它返回每个输入字节生成的平均字符数。该启发式值可用于估计给定输入序列所需的输出缓冲区的大小。

语法

public final float averageCharsPerByte()

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

返回值:该函数返回每个输入字节将产生的平均字符数。

下面是上述函数的实现:

方案一:

// Java program to demonstrate
// the above function
  
import java.nio.charset.*;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Gets the charset
        Charset charset = Charset.forName("ISO-2022-CN");
  
        // Get the CharsetDecoder
        CharsetDecoder decoder = charset.newDecoder();
  
        // Prints the averageCharsPerByte
        System.out.println("The averageCharsPerByte "
                           + "for ISO-2022-CN is "
                           + decoder.averageCharsPerByte());
    }
}
输出:
The averageCharsPerByte for ISO-2022-CN is 1.0

方案二:

// Java program to demonstrate
// the above function
  
import java.nio.charset.*;
import java.util.Iterator;
import java.util.Map;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Gets the charset
        Charset charset = Charset.forName("x-windows-949");
  
        // Get the CharsetDecoder
        CharsetDecoder decoder = charset.newDecoder();
  
        // Prints the averageCharsPerByte
        System.out.println("The averageCharsPerByte "
                           + "for ISO-2022-CN is "
                           + decoder.averageCharsPerByte());
    }
}
输出:
The averageCharsPerByte for ISO-2022-CN is 0.5

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