Example usage for java.io InputStreamReader close

List of usage examples for java.io InputStreamReader close

Introduction

In this page you can find the example usage for java.io InputStreamReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

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

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

    // 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);//from  w w  w  . j  av  a  2s .co  m

}

From source file:Main.java

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

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

    // 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);/*from   w w  w  .ja va  2s . co  m*/

}

From source file:Main.java

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);//w  ww  .ja  v  a 2  s. c o m

}

From source file:Main.java

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

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

    // 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);//from ww  w  . j a  v  a 2 s . c  om

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    // new input stream reader is created
    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // the name of the character encoding returned
    String s = isr.getEncoding();
    System.out.print("Character Encoding: " + s);
    isr.close();
}

From source file:ReadConsole.java

public static void main(String args[]) throws Exception {
    InputStreamReader isr = new InputStreamReader(System.in);

    BufferedReader br = new BufferedReader(isr);

    String s;// ww w.ja  v  a 2  s .co m
    while ((s = br.readLine()) != null) {
        System.out.println(s.length());
    }
    isr.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    int i;/*from   w  w w  .  j  ava  2 s  . c om*/
    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // read till the end of the file
    while ((i = isr.read()) != -1) {
        // int to character
        char c = (char) i;
        System.out.println("Character Read: " + c);
    }
    isr.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*  www  .ja v  a2  s . c  om*/
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile, encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    try {/*from w ww  .jav a2s.  c  o m*/
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile,

                encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:CharsetDetector.java

public static void main(String[] args) {
    File f = new File("example.txt");

    String[] charsetsToBeTested = { "UTF-8", "windows-1253", "ISO-8859-7" };

    CharsetDetector cd = new CharsetDetector();
    Charset charset = cd.detectCharset(f, charsetsToBeTested);

    if (charset != null) {
        try {//from  w  w  w . j  a  v a2  s.co m
            InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
            int c = 0;
            while ((c = reader.read()) != -1) {
                System.out.print((char) c);
            }
            reader.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    } else {
        System.out.println("Unrecognized charset.");
    }
}