Java CharsetEncoder malformedInputAction()方法及示例

Java CharsetEncoder malformedInputAction()方法及示例

malformedInputAction() 方法是 java.nio.charset.CharsetEncoder 的一个内置方法,用于返回该编码器当前对畸形输入错误的操作。这样返回的CodingErrorAction有三种类型:IGNORE、REPLACE和REPORT。

语法:

public CodingErrorAction malformedInputAction()

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

返回值 :该函数返回当前的畸形输入动作,该动作从不为空。

下面是上述函数的实现。

程序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.malformedInputAction());
    }
}

输出:

REPORT

程序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("US-ASCII").newEncoder();
  
        // Prints CodingErrorAction
        System.out.println(encoder.malformedInputAction());
    }
}

输出:

REPORT

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南