Charset newEncoder() in Java

newEncoder(): This method is available in java.nio.charset.Charset class of Java.

Syntax:

CharsetEncoder java.nio.charset.Charset.newEncoder()

This method constructs a new encoder for this charset.

Parameters: NA

Returns: A new encoder for this charset

Throws:

UnsupportedOperationException - If this charset does not support encoding.

For Example:

Charset cs = Charset.forName("UTF-8")

cs.newEncoder() = > It returns sun.nio.cs.UTF_8$Encoder@76ccd017.

Approach

Java

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class CharsetnewEncoder {
    public static void main(String[] args) {

        Charset cs = Charset.forName("UTF-8");

        CharsetEncoder encode = cs.newEncoder();
        System.out.println(encode.toString());
    }
}

Output:

sun.nio.cs.UTF_8$Encoder@76ccd017

No comments:

Post a Comment