Example usage for java.io InputStreamReader InputStreamReader

List of usage examples for java.io InputStreamReader InputStreamReader

Introduction

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

Prototype

public InputStreamReader(InputStream in, CharsetDecoder dec) 

Source Link

Document

Creates an InputStreamReader that uses the given charset decoder.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("infilename"), "UTF8"));
    String str = in.readLine();/*from ww  w  . java2 s  .  c  o m*/
    System.out.println(str);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("infilename"), "8859_1"));
    String str = in.readLine();//from   w ww  .  ja  v a2s .c  o  m
    System.out.println(str);
}

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();//from  ww  w . j  ava2  s  . c om
    System.out.print("close() invoked");

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

}

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();//from ww  w .j av  a  2  s .  c om
    System.out.print("close() invoked");

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

}

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();/*  w  w w  . j  av a 2  s .  c o m*/
    System.out.print("close() invoked");

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

}

From source file:Main.java

public static void main(final String[] args) throws IOException {
    File infile = new File("/tmp/utf16.txt");
    FileInputStream inputStream = new FileInputStream(infile);
    Reader in = new InputStreamReader(inputStream, "UTF-16");
    int read;/*from w ww  .  java2  s .  com*/
    while ((read = in.read()) != -1) {
        System.out.print(Character.toChars(read));
    }
    in.close();
}

From source file:Converter.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(new File("input.txt"));
    BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

    FileOutputStream fos = new FileOutputStream(new File("output.txt"));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

    int len = 80;
    char buf[] = new char[len];

    int numRead;//  ww w  .j a  va 2  s .  com
    while ((numRead = in.read(buf, 0, len)) != -1)
        out.write(buf, 0, numRead);
    out.close();
    in.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   www  .ja  va 2s  .com*/
        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:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:h2:mem:");
    Statement s = con.createStatement();
    s.execute("CREATE TABLE Table1 (Column1 CLOB)");

    InputStream is = new FileInputStream("data.txt");
    Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
    PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
    ps.setCharacterStream(1, rdr);//  w  w w .j  a  v  a  2 s .co m
    ps.executeUpdate();

    ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
    int rowNumber = 0;
    while (rs.next()) {
        String str = rs.getString("Column1");
        System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
    }
    rs.close();
    con.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//  ww w  . ja v a  2s.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());
    }
}