Java - InputStreamReader getEncoding() method



Description

The Java InputStreamReader getEncoding() method returns the name of the character encoding used by the reader. If the encoding was not specified, it returns the default encoding of the JVM. Common encodings: "UTF-8", "ISO-8859-1", "US-ASCII", "UTF-16", etc.

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 - Usage of InputStreamReader getEncoding() method

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

InputStreamReaderDemo.java

 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("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(); } } } 

Output(Assuming test.txt contains "ABCDE")

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

 Character Encoding: UTF8 

Example - Getting Default Encoding of InputStreamReader

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

InputStreamReaderDemo.java

 package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) { System.out.println("Encoding used: " + reader.getEncoding()); } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Encoding used: UTF8 

Explanation

  • Creates an InputStreamReader with default encoding.

  • Calls getEncoding() to print the encoding used.

  • The encoding depends on the JVM default (e.g., "UTF8" or "Cp1252" on Windows).

Example - Specifying Encoding in InputStreamReader

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

InputStreamReaderDemo.java

 package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"), "ISO-8859-1")) { System.out.println("Encoding used: " + reader.getEncoding()); } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Encoding used: ISO8859_1 

Explanation

  • Creates an InputStreamReader with "ISO-8859-1" encoding.

  • Calls getEncoding() to confirm the encoding used.

  • Ensures that the correct encoding is applied when reading non-ASCII characters.

java_io_inputstreamreader.htm
Advertisements