Java IO Tutorial - Java InputStreamReader(InputStream in, String charsetName) Constructor








Syntax

InputStreamReader(InputStream in, String charsetName) constructor from InputStreamReader has the following syntax.

public InputStreamReader(InputStream in,
                                     String charsetName)
                   throws UnsupportedEncodingException

Example

In the following code shows how to use InputStreamReader.InputStreamReader(InputStream in, String charsetName) constructor.

//  w w  w . j  a  v  a 2s .  c  o m

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Main {
  public static void main(String[] args) throws IOException {

    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis,Charset.defaultCharset().name());

    // input stream reader is closed
    isr.close();
    System.out.print("close() invoked");

    // read() called after closed method
    int i = isr.read();
    char c = (char) i;
    System.out.println(c);

  }
}