CharsetDecoder detectedCharset() method in Java with Examples Last Updated : 27 Jun, 2019 Comments Improve Suggest changes Like Article Like Report The detectedCharset() method is a built-in method of the java.nio.charset.CharsetDecoder class which retrieves the charset that has been detected by this decoder. The default implementation of this method always throws an UnsupportedOperationException. It should be overridden by auto-detecting decoders to return true once the input charset has been determined. Syntax: public Charset detectedCharset() Parameters: The function does not accepts any parameter. Return Value: The function returns the charset that has been detected by this decoder. Exception: It throws UnsupportedOperationException if this decoder does not implement an auto-detecting charset and IllegalStateException if insufficient bytes have been read to determine a charset Below is the implementation of the above function: Program 1: Java // 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 CharsetDecoder System.out.println("CharsetDecoder: " + decoder); try { System.out.println("detected Charset: " + decoder.detectedCharset()); } catch (Exception e) { System.out.println(e); } } } Output: CharsetDecoder: sun.nio.cs.ext.ISO2022_CN$Decoder@232204a1 java.lang.UnsupportedOperationException Program 2: Java // 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 CharsetDecoder System.out.println("CharsetDecoder: " + decoder); try { System.out.println("detected Charset: " + decoder.detectedCharset()); } catch (Exception e) { System.out.println(e); } } } Output: CharsetDecoder: sun.nio.cs.ext.DoubleByte$Decoder@232204a1 java.lang.UnsupportedOperationException Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/charset/CharsetDecoder.html#detectedCharset-- Comment More infoAdvertise with us Next Article CharsetDecoder detectedCharset() method in Java with Examples S ShubhamMaurya3 Follow Improve Article Tags : Misc Java Java-Functions Java-CharsetDecoder Java-nio-charset package +1 More Practice Tags : JavaMisc Similar Reads Charset defaultCharset() method in Java with Examples The defaultCharset() method is a built-in method of the java.nio.charset which returns the charset object for the default charset. The default charset is basically determined by the Java virtual machine and it basically depends on the charset which is in the underlying operating system of the machin 1 min read Charset canEncode() method in Java with Examples The canEncode() method is a built-in method of the java.nio.charset checks whether a particular charset supports encoding or not. Almost every charset can encode, except a few. Syntax: public boolean canEncode() Parameters: The function does not accepts any parameter. Return Value: The function retu 3 min read CharsetEncoder charset() method in Java with Examples The charset() method is a built-in method of the java.nio.charset.CharsetEncoder returns the charset that created this encoder. Syntax: public final Charset charset() Parameters: The function does not accepts any parameter. Return Value: The function returns this encoder's charset. Below is the impl 1 min read CharsetDecoder reset() method in Java with Examples The reset() method is a built-in method of the java.nio.charset.CharsetDecoder class which resets this CharsetDecoder and clears its internal state. Syntax: public final CharsetDecoder reset() Parameters: The function does not accepts any parameter. Return Value: The function returns this CharsetDec 1 min read Charset compareTo() method in Java with Examples The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second w 1 min read Like