Home » Java programming language

Java InputStreamReader getEncoding() Method with Example

InputStreamReader Class getEncoding() method: Here, we are going to learn about the getEncoding() method of InputStreamReader Class with its syntax and example.
Submitted by Preeti Jain, on April 13, 2020

InputStreamReader Class getEncoding() method

  • getEncoding() method is available in java.io package.
  • getEncoding() method is used to get the encoding name avail for this InputStreamReader stream and it returns a historical encoding name when it exists otherwise it returns canonical encoding name.
  • getEncoding() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getEncoding() method does not throw an exception at the time of getting encoding.

Syntax:

    public String getEncoding();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is String, it gets historical character encoding name when exists otherwise it returns canonical encoding name or it may return null when this stream has been closed.

Example:

// Java program to demonstrate the example 
// of String getEncoding() method
// of InputStreamReader

import java.io.*;

public class Demo1 {
 public static void main(String[] args) throws Exception {
  InputStream is_stm = null;
  InputStreamReader isr_stm = null;
  int val = 0;

  try {
   // Instantiates FileInputStream and InputStreamReader 
   is_stm = new FileInputStream("D:\\includehelp.txt");
   isr_stm = new InputStreamReader(is_stm);

   // By using getEncoding() method is to         
   // get the character encoding used by the 
   // stream isr_stm
   String encoding = isr_stm.getEncoding();
   System.out.println("isr_stm.getEncoding(): " + encoding);

  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (is_stm != null) {
    is_stm.close();

    if (isr_stm != null) {
     isr_stm.close();
    }
   }
  }
 }
}

Output

isr_stm.getEncoding(): Cp1252


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.