Example usage for java.io StreamTokenizer TT_EOF

List of usage examples for java.io StreamTokenizer TT_EOF

Introduction

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

Prototype

int TT_EOF

To view the source code for java.io StreamTokenizer TT_EOF.

Click Source Link

Document

A constant indicating that the end of the stream has been read.

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   ww  w .  jav a2s .c om*/
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }

    fr.close();
}

From source file:MainClass.java

public static void main(String args[]) {

    try {//  w w  w  .  ja  va2  s .c om
        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:
                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[] args) throws Exception {
    BufferedReader in = null;/*from   www  .  j a  va 2 s  .  c om*/
    in = new BufferedReader(new FileReader("fileeditor.txt"));
    StreamTokenizer st = new StreamTokenizer(in);
    st.eolIsSignificant(false);
    // remove comment handling
    st.slashSlashComments(false);
    st.slashStarComments(false);

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        if (st.ttype == StreamTokenizer.TT_NUMBER) {
            // the default is to treat numbers differently than words
            // also the numbers are doubles
            System.out.println((int) st.nval);
        } else {
            System.out.println(st.sval);
        }
    }
}

From source file:InputOutputDemo.java

public static void main(String[] a) throws Exception {
    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));
    pwr.print(4711);/*from  ww  w .  j  ava 2  s . co  m*/
    pwr.print(' ');
    pwr.print("Java Source and Support at www.java2s.com");
    pwr.close();

    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
        System.out.println(stok.sval);
        tok = stok.nextToken();
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {/*  w ww.  ja  v a  2  s  .  c o m*/
        int token = inStream.nextToken();
        switch (token) {
        case StreamTokenizer.TT_EOF:
            System.out.println("EOF encountered.");
            eof = true;
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("EOL encountered.");
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word: " + inStream.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number: " + inStream.nval);
            break;
        default:
            System.out.println((char) token + " encountered.");
            if (token == '!')
                eof = true;
        }
    } while (!eof);
}

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 w ww.  ja va2 s . c  o  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:MainClass.java

public static void main(String args[]) {

    try {/*from  w  w  w  . j  a v a2s  . com*/

        // Create a file reader
        FileReader fr = new FileReader(args[0]);

        // Create a buffered reader
        BufferedReader br = new BufferedReader(fr);

        // Create a stream tokenizer
        StreamTokenizer st = new StreamTokenizer(br);

        // Process tokens
        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:
                System.out.println(st.lineno() + ") " + (char) st.ttype);
            }
        }

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

From source file:MainClass.java

public static void main(String args[]) {

    try {//from   w  ww  .j av  a 2s  .c o m
        FileReader fr = new FileReader(args[0]);

        BufferedReader br = new BufferedReader(fr);

        StreamTokenizer st = new StreamTokenizer(br);

        // Consider end-of-line as a token
        st.eolIsSignificant(true);

        // Declare variable to count lines
        int lines = 1;

        // Process tokens
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            switch (st.ttype) {
            case StreamTokenizer.TT_EOL:
                ++lines;
            }
        }

        System.out.println("There are " + lines + " lines");

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

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);/* w  ww. j  a  va2 s  .c  o m*/
    StreamTokenizer stok = new StreamTokenizer(lnr);
    stok.parseNumbers();
    stok.eolIsSignificant(true);
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        int lineno = lnr.getLineNumber();
        double sum = 0;
        while (stok.ttype != StreamTokenizer.TT_EOL) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER)
                sum += stok.nval;
            stok.nextToken();
        }
        System.out.println("Sum of line " + lineno + " is " + sum);
        stok.nextToken();
    }
}

From source file:SumFile.java

static void sumfile(String filename) throws IOException {
    Reader r = new BufferedReader(new FileReader(filename));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();//from   w w  w. j  ava2 s  .  c o m
    double sum = 0;
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        if (stok.ttype == StreamTokenizer.TT_NUMBER)
            sum += stok.nval;
        else
            System.out.println("Nonnumber: " + stok.sval);
        stok.nextToken();
    }
    System.out.println("The file sum is " + sum);
}