UnsupportedEncodingException in Java

UnsupportedEncodingException:

java.io.UnsupportedEncodingException

Character Encoding is not supported.

Approach

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class UnsupportedEncodingExceptionExample {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream in = new FileInputStream(file);
        String charsetName = "UTF-81919";
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(in, charsetName);
        } catch (UnsupportedEncodingException e) {
            System.out.println("UnsupportedEncodingException occurs");
            return;
        }

        System.out.println(inputStreamReader);
        inputStreamReader.close();
    }
}

Output:

UnsupportedEncodingException occurs


Constructor of UnsupportedEncodingException

Example 1:

java.io.UnsupportedEncodingException.UnsupportedEncodingException()

Constructs an UnsupportedEncodingException without a detailed message.

Java

import java.io.UnsupportedEncodingException;

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

        UnsupportedEncodingException unsupportedEncodingException =
new UnsupportedEncodingException();

        System.out.println(unsupportedEncodingException);
    }
}

Output:

java.io.UnsupportedEncodingException


Example 2:

java.io.UnsupportedEncodingException.UnsupportedEncodingException(String s)

Constructs an UnsupportedEncodingException with a detailed message.

Parameters:

1. s Describe the reason for the exception.

Java

import java.io.UnsupportedEncodingException;

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

        UnsupportedEncodingException unsupportedEncodingException =
new UnsupportedEncodingException("message");

        System.out.println(unsupportedEncodingException);
    }
}

Output:

java.io.UnsupportedEncodingException: message


Some other Exceptions of java.io

EOFExceptionSignals that an end of file or end of the stream has been reached unexpectedly during input.

FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified path name has failed.

InterruptedIOExceptionSignals that an I/O operation has been interrupted. 

InvalidClassExceptionThrown when the Serialization runtime detects.

InvalidObjectExceptionIndicates that one or more deserialized objects failed validation tests.

IOErrorThrown when a serious I/O error has occurred.

IOExceptionSignals that an I/O exception of some sort has occurred.

NotActiveExceptionThrown when serialization or deserialization is not active.

NotSerializableExceptionThrown when an instance is required to have a Serializable interface.

ObjectStreamExceptionSuperclass of all exceptions specific to Object Stream classes.

OptionalDataExceptionException indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream.

StreamCorruptedExceptionThrown when control information that was read from an object stream violates internal consistency checks.

SyncFailedExceptionSignals that a sync operation has failed.

UncheckedIOExceptionWraps an IOException with an unchecked exception.

UTFDataFormatExceptionSignals that a malformed string in modified UTF-8 format has been read in a data input stream or by any class that implements the data input interface.

WriteAbortedExceptionSignals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment