Example usage for java.io FileReader close

List of usage examples for java.io FileReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("hello.txt");
    FileReader fr = new FileReader(f);

    char[] c = new char[(int) f.length()];
    char[] cnew = new char[(int) f.length()];
    StringBuffer sbuf = new StringBuffer();
    fr.read(c, 0, (int) f.length());

    int len = (int) f.length();

    for (int i = 0, j = len - 1; i < len; i++, j--) {
        cnew[i] = c[j];//from ww w . j  a va2  s.c  o m
        sbuf.append(cnew[i]);
    }

    System.out.println(sbuf.toString());
    fr.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("Main.java");
    FileReader fr = new FileReader(f);

    char[] c = new char[(int) f.length()];
    char[] cnew = new char[(int) f.length()];
    StringBuffer sbuf = new StringBuffer();
    fr.read(c, 0, (int) f.length());

    int len = (int) f.length();

    for (int i = 0, j = len - 1; i < len; i++, j--) {
        cnew[i] = c[j];/*  w ww.  java 2  s . co m*/
        sbuf.append(cnew[i]);
    }

    System.out.println(sbuf.toString());
    fr.close();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    st.eolIsSignificant(true);/*from  ww w . j a  v  a2s.com*/
    int lines = 1;

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_EOL:
            ++lines;
        }
    }
    System.out.println("There are " + lines + " lines");
    fr.close();
}

From source file:MainClass.java

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

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();/*from ww w.  j  a v  a 2 s . c o  m*/

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);
    // Set delimiters to space and comma.
    // ", *" tells Scanner to match a comma and zero or more spaces as
    // delimiters.

    src.useDelimiter(", *");

    // Read and sum numbers.
    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            System.out.println(src.nextDouble());
        } else {
            break;
        }
    }
    fin.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    try {/*from w ww .j  ava 2  s . c  om*/
        int counts[] = new int[10];
        FileReader fr = new FileReader(args[0]);

        int i;
        while ((i = fr.read()) != -1) {
            char c = (char) i;
            int k = c - '0';
            if (k >= 0 && k < 10)
                ++counts[k];
        }

        // Display digit counts
        for (int j = 0; j < 10; j++) {
            char c = (char) ('0' + j);
            System.out.print(c + "=");
            System.out.print(counts[j] + "; ");
        }

        fr.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (Id int, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");

    FileReader reader = new FileReader(file);
    pstmt.setCharacterStream(1, reader);

    pstmt.execute();/*from w w w  . j a v  a2  s .com*/

    ResultSet resultSet = pstmt.executeQuery("select b from survey ");

    File data = new File("C:\\a.txt");
    Reader dataReader = resultSet.getCharacterStream(1);
    FileWriter writer = new FileWriter(data);
    char[] buffer = new char[1];
    while (dataReader.read(buffer) > 0) {
        writer.write(buffer);
    }
    writer.close();

    reader.close();
    st.close();
    conn.close();
}

From source file:TabFilter.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);

    FileWriter fw = new FileWriter(args[1]);
    BufferedWriter bw = new BufferedWriter(fw);

    // Convert tab to space characters
    String s;//from w w w . j a  v  a  2 s.  co m
    while ((s = br.readLine()) != null) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\t')
                c = ' ';
            bw.write(c);
        }
    }

    bw.flush();
    fr.close();
    fw.close();
}

From source file:MainClass.java

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

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
    fout.close();/*from   ww w  .j av  a 2  s  . c o m*/

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {
    // Write output to a file.
    FileWriter fout = new FileWriter("test.txt");
    fout.write("int: 1  double 1.0  boolean true");
    fout.close();//from ww w . j a v a 2  s.co  m

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            System.out.println("int: " + src.nextInt());
        } else if (src.hasNextDouble()) {
            System.out.println("double: " + src.nextDouble());
        } else if (src.hasNextBoolean()) {
            System.out.println("boolean: " + src.nextBoolean());
        } else {
            System.out.println(src.next());
        }
    }
    fin.close();
}

From source file:Main.java

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

    int count = 0;
    double sum = 0.0;

    FileWriter fout = new FileWriter("test.txt");

    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();/*from ww w .  j  a  v a2  s . com*/

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(", *");

    while (src.hasNext()) {
        if (src.hasNextDouble()) {
            sum += src.nextDouble();
            count++;
        } else {
            String str = src.next();
            if (str.equals("done"))
                break;
            else {
                System.out.println("File format error.");
                return;
            }
        }
    }

    fin.close();
    System.out.println("Average is " + sum / count);
}