Example usage for java.io StreamTokenizer StreamTokenizer

List of usage examples for java.io StreamTokenizer StreamTokenizer

Introduction

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

Prototype

public StreamTokenizer(Reader r) 

Source Link

Document

Create a tokenizer that parses the given character stream.

Usage

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);/*  ww w .  j a  va 2s  .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();// w ww .  j a v a 2s .  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);
}

From source file:Counter.java

public WordCount1(String filename) throws FileNotFoundException {
    try {/* w w  w .  ja  v  a 2  s.  c om*/
        file = new FileReader(filename);
        st = new StreamTokenizer(new BufferedReader(file));
        st.ordinaryChar('.');
        st.ordinaryChar('-');
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.denimgroup.threadfix.framework.filefilter.ClassAnnotationBasedFileFilter.java

@Override
public boolean accept(@Nullable File file) {
    boolean returnValue = false;
    boolean hasArroba = false;

    if (file != null && file.exists() && file.isFile() && file.getName().endsWith(".java")) {
        Reader reader = null;/*from ww w . j a v a2 s .c o m*/

        try {

            reader = new InputStreamReader(new FileInputStream(file), "UTF-8");

            StreamTokenizer tokenizer = new StreamTokenizer(reader);
            tokenizer.slashSlashComments(true);
            tokenizer.slashStarComments(true);

            while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if (hasArroba && tokenizer.sval != null && getClassAnnotations().contains(tokenizer.sval)) {
                    returnValue = true;
                    break;
                } else if (tokenizer.sval != null && tokenizer.sval.equals("class")) {
                    // we've gone too far
                    break;
                }

                hasArroba = tokenizer.ttype == '@';
            }
        } catch (IOException e) {
            log.warn("Encountered IOException while tokenizing file.", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("Encountered IOException while attempting to close file.", e);
                }
            }
        }
    }

    return returnValue;
}

From source file:eu.scape_project.pt.util.PipedArgsParser.java

/**
 * Public interface for parsing a command line. The form of the
 * command line should be (--key="value")* (&lt stdinfile)? (&gt stdoutfile)?.
 * Results can be fetched via getters./*from w  w  w .j  a v  a 2 s .  c o m*/
 * @param String strCmdLine input String
 * @throws IOException 
 */
public void parse(String strCmdLine) throws IOException {

    strStdinFile = "";
    strStdoutFile = "";
    tokenizer = new StreamTokenizer(new StringReader(strCmdLine));

    Varbox s = S();

    strStdinFile = s.stdin;
    strStdoutFile = s.stdout;
    commands = s.commands.toArray(new Command[0]);

}

From source file:eu.scape_project.pt.util.ArgsParser.java

/**
 * Public interface for parsing a command line. The form of the
 * command line should be (--key="value")* (&lt stdinfile)? (&gt stdoutfile)?.
 * Results can be fetched via getters./*from w w w.  j  a  v a  2s. c om*/
 * @param String strCmdLine input String
 * @throws IOException 
 */
public void parse(String strCmdLine) throws IOException {

    mapArguments = new HashMap<String, String>();
    strStdinFile = "";
    strStdoutFile = "";
    tokenizer = new StreamTokenizer(new StringReader(strCmdLine));

    root();
}

From source file:ScanStreamTok.java

/** Construct a file scanner by name */
public ScanStreamTok(String fileName) throws IOException {
    tf = new StreamTokenizer(new FileReader(fileName));
}

From source file:ScanStreamTok.java

/** Construct a file scanner by existing Reader */
public ScanStreamTok(Reader rdr) throws IOException {
    tf = new StreamTokenizer(rdr);
}

From source file:Command.java

/**
 * This static method creates a Command using the specified target object,
 * and the specified string. The string should contain method name followed
 * by an optional parenthesized comma-separated argument list and a
 * semicolon. The arguments may be boolean, integer or double literals, or
 * double-quoted strings. The parser is lenient about missing commas,
 * semicolons and quotes, but throws an IOException if it cannot parse the
 * string./*w  ww.  j av a  2 s  .  c o m*/
 */
public static Command parse(Object target, String text) throws IOException {
    String methodname; // The name of the method
    ArrayList args = new ArrayList(); // Hold arguments as we parse them.
    ArrayList types = new ArrayList(); // Hold argument types.

    // Convert the string into a character stream, and use the
    // StreamTokenizer class to convert it into a stream of tokens
    StreamTokenizer t = new StreamTokenizer(new StringReader(text));

    // The first token must be the method name
    int c = t.nextToken(); // read a token
    if (c != t.TT_WORD) // check the token type
        throw new IOException("Missing method name for command");
    methodname = t.sval; // Remember the method name

    // Now we either need a semicolon or a open paren
    c = t.nextToken();
    if (c == '(') { // If we see an open paren, then parse an arg list
        for (;;) { // Loop 'till end of arglist
            c = t.nextToken(); // Read next token

            if (c == ')') { // See if we're done parsing arguments.
                c = t.nextToken(); // If so, parse an optional semicolon
                if (c != ';')
                    t.pushBack();
                break; // Now stop the loop.
            }

            // Otherwise, the token is an argument; figure out its type
            if (c == t.TT_WORD) {
                // If the token is an identifier, parse boolean literals,
                // and treat any other tokens as unquoted string literals.
                if (t.sval.equals("true")) { // Boolean literal
                    args.add(Boolean.TRUE);
                    types.add(boolean.class);
                } else if (t.sval.equals("false")) { // Boolean literal
                    args.add(Boolean.FALSE);
                    types.add(boolean.class);
                } else { // Assume its a string
                    args.add(t.sval);
                    types.add(String.class);
                }
            } else if (c == '"') { // If the token is a quoted string
                args.add(t.sval);
                types.add(String.class);
            } else if (c == t.TT_NUMBER) { // If the token is a number
                int i = (int) t.nval;
                if (i == t.nval) { // Check if its an integer
                    // Note: this code treats a token like "2.0" as an int!
                    args.add(new Integer(i));
                    types.add(int.class);
                } else { // Otherwise, its a double
                    args.add(new Double(t.nval));
                    types.add(double.class);
                }
            } else { // Any other token is an error
                throw new IOException(
                        "Unexpected token " + t.sval + " in argument list of " + methodname + "().");
            }

            // Next should be a comma, but we don't complain if its not
            c = t.nextToken();
            if (c != ',')
                t.pushBack();
        }
    } else if (c != ';') { // if a method name is not followed by a paren
        t.pushBack(); // then allow a semi-colon but don't require it.
    }

    // We've parsed the argument list.
    // Next, convert the lists of argument values and types to arrays
    Object[] argValues = args.toArray();
    Class[] argtypes = (Class[]) types.toArray(new Class[argValues.length]);

    // At this point, we've got a method name, and arrays of argument
    // values and types. Use reflection on the class of the target object
    // to find a method with the given name and argument types. Throw
    // an exception if we can't find the named method.
    Method method;
    try {
        method = target.getClass().getMethod(methodname, argtypes);
    } catch (Exception e) {
        throw new IOException("No such method found, or wrong argument " + "types: " + methodname);
    }

    // Finally, create and return a Command object, using the target object
    // passed to this method, the Method object we obtained above, and
    // the array of argument values we parsed from the string.
    return new Command(target, method, argValues);
}

From source file:keel.Algorithms.Decision_Trees.C45.C45.java

/** Constructor.
 *
 * @param paramFile      The parameters file.
 *
 * @throws Exception   If the algorithm cannot be executed.
 *//*from   ww  w. ja  va2s  . c  o  m*/
public C45(String paramFile) throws Exception {
    try {

        // starts the time
        long startTime = System.currentTimeMillis();

        /* Sets the options of the execution from text file*/
        StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new FileReader(paramFile)));
        initTokenizer(tokenizer);
        setOptions(tokenizer);

        /* Sets the options from XML file */
        /** para commons.configuration
         XMLConfiguration config = new XMLConfiguration(paramFile);
           setOptions( config );
         */

        /* Initializes the dataset. */
        modelDataset = new Dataset(modelFileName, true);
        trainDataset = new Dataset(trainFileName, false);
        testDataset = new Dataset(testFileName, false);

        priorsProbabilities = new double[modelDataset.numClasses()];
        priorsProbabilities();
        marginCounts = new double[marginResolution + 1];

        // generate the tree
        generateTree(modelDataset);

        printTrain();
        printTest();
        printResult();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.exit(-1);
    }
}