Java.io.InputStreamReader.getEncoding() Method

Advertisements


Description

The java.io.InputStreamReader.getEncoding() method returns the name of the character encoding used by this stream. If the encoding has an historical name then the name is returned; else the encoding's canonical name is returned.

Declaration

Following is the declaration for java.io.InputStreamReader.getEncoding() method:

public String getEncoding()

Parameters

  • NA

Return Value

The historical name of this encoding, or null if the stream has been closed.

Exception

  • NA

Example

The following example shows the usage of java.io.InputStreamReader.getEncoding() method.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      InputStreamReader isr =null;
      String s;
      
      try {
         // new input stream reader is created 
         fis = new FileInputStream("C:/test.txt");
         isr = new InputStreamReader(fis);
         
         // the name of the character encoding returned
         s=isr.getEncoding();
         System.out.print("Character Encoding: "+s);
         
      } catch (Exception e) {
      
         // print error
         System.out.print("The stream is already closed");
      } finally {
         
         // closes the stream and releases resources associated
         if(fis!=null)
            fis.close();
         if(isr!=null)
            isr.close();
      }   
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDE

Let us compile and run the above program, this will produce the following result:

Character Encoding: Cp1252



Advertisements
Advertisements