Example usage for java.io FileReader FileReader

List of usage examples for java.io FileReader FileReader

Introduction

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

Prototype

public FileReader(FileDescriptor fd) 

Source Link

Document

Creates a new FileReader , given the FileDescriptor to read, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:StreamTokenizerDemo.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.ordinaryChar('.');
    st.wordChars('\'', '\'');

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
            System.out.println(st.lineno() + ") " + st.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println(st.lineno() + ") " + st.nval);
            break;
        default:/*from   www  . j  av a 2  s.  c om*/
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }

    fr.close();
}

From source file:MainClass.java

public static void main(String args[]) {

    try {// ww w. j a  v a  2 s  .c om

        FileReader fr = new FileReader(args[0]);

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

        int i;
        while ((i = fr.read()) != -1) {
            fw.write(i);
        }
        fw.close();

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileReader rd = new FileReader("filename.java");
    StreamTokenizer st = new StreamTokenizer(rd);

    st.parseNumbers();//from ww  w  .  ja  v  a2 s .  co  m
    st.wordChars('_', '_');
    st.eolIsSignificant(true);
    st.ordinaryChars(0, ' ');
    st.slashSlashComments(true);
    st.slashStarComments(true);

    int token = st.nextToken();
    while (token != StreamTokenizer.TT_EOF) {
        token = st.nextToken();
        switch (token) {
        case StreamTokenizer.TT_NUMBER:
            double num = st.nval;
            System.out.println(num);
            break;
        case StreamTokenizer.TT_WORD:
            String word = st.sval;
            System.out.println(word);
            break;
        case '"':
            String dquoteVal = st.sval;
            System.out.println(dquoteVal);
            break;
        case '\'':
            String squoteVal = st.sval;
            System.out.println(squoteVal);
            break;
        case StreamTokenizer.TT_EOL:
            break;
        case StreamTokenizer.TT_EOF:
            break;
        default:
            char ch = (char) st.ttype;
            System.out.println(ch);
            break;
        }
    }
    rd.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String first, last = null, line;
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    first = in.readLine();/*from www  .j a  va 2 s. c  o m*/
    while ((line = in.readLine()) != null) {
        if (line != null) {
            last = line;
        }
    }
    System.out.println(first + "\n" + last);
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("data.csv");
    FileReader fr = new FileReader(file);
    LineNumberReader lnr = new LineNumberReader(fr);
    // lnr.setLineNumber(400);
    String line = "";
    while ((line = lnr.readLine()) != null) {
        System.out.println("Line Number " + lnr.getLineNumber() + ": " + line);
    }//from w ww  .ja  v  a 2 s .  c  o  m
    fr.close();
    lnr.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    try {//  w ww  . j  a v a 2s  .c o  m
        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:WordCount.java

public static void main(String args[]) throws Exception {
    int words = 0;
    int lines = 0;
    int chars = 0;

    FileReader fr = new FileReader("yourFile.txt");
    int c = 0;/*from  w  w  w.j  av  a  2 s  .  c  o m*/
    boolean lastWhite = true;
    String whiteSpace = " \t\n\r";

    while ((c = fr.read()) != -1) {
        chars++;
        if (c == '\n') {
            lines++;
        }
        int index = whiteSpace.indexOf(c);
        if (index == -1) {
            if (lastWhite == true) {
                ++words;
            }
            lastWhite = false;
        } else {
            lastWhite = true;
        }
    }
    if (chars != 0) {
        ++lines;
    }
}

From source file:Main.java

public static void main(String args[]) {

    try {// w w w.  j  a v  a  2  s. com
        FileReader fr = new FileReader(args[0]);
        BufferedReader br = new BufferedReader(fr);
        StreamTokenizer st = new StreamTokenizer(br);

        st.ordinaryChar('.');

        st.wordChars('\'', '\'');

        while (st.nextToken() != StreamTokenizer.TT_EOL) {
            switch (st.ttype) {
            case StreamTokenizer.TT_WORD:
                System.out.println(st.lineno() + ") " + st.sval);
                break;
            case StreamTokenizer.TT_NUMBER:
                System.out.println(st.lineno() + ") " + st.nval);
                break;
            default:
                System.out.println(st.lineno() + ") " + (char) st.ttype);
            }
        }

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("C:\\test.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);

    StringBuffer sb = new StringBuffer();
    String eachLine = br.readLine();

    while (eachLine != null) {
        sb.append(eachLine);/*from ww w. ja  v  a2 s .com*/
        sb.append("\n");
        eachLine = br.readLine();
    }
    System.out.println(sb.toString());
}

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;// ww  w.j a va  2s .  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();
}