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:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]//from   www .  j a va  2s.  c  o  m
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param fileName
 *            The path to the text file to read the data from
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(String fileName, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    try (Reader r = new BufferedReader(new FileReader(fileName))) {

        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        int currentColumn = 0;
        double range0 = 0;
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from file " + fileName + ": " + stok.sval,
                        LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Could not find file " + fileName + " from which to read histogram", e);
    } catch (IOException e) {
        printStack(e);
    }

}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]//w  ww  .  ja va2s.co m
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param plot
 *            The plot produced by a plugin
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(PluginIOCells plot, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    byte[] protobufArray = (byte[]) plot.getProperty("Protobuf");
    if (protobufArray == null)
        return;
    Reader r = new StringReader(new String(protobufArray));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();
    int currentColumn = 0;
    double range0 = 0;
    try {
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from string " + plot.getProperty("Protobuf") + ": "
                        + stok.sval, LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (IOException e) {
        printStack(e);
        return;
    }

}

From source file:uk.ac.leeds.ccg.andyt.projects.fluvialglacial.SlopeAreaAnalysis.java

protected TreeMap<Integer, Object[]> readSwissData(File fileIn) {
    TreeMap<Integer, Object[]> result;
    result = new TreeMap<Integer, Object[]>();
    BufferedReader br;/* w  w  w .  ja  v a  2 s . com*/
    br = Generic_StaticIO.getBufferedReader(fileIn);
    StreamTokenizer st;
    st = new StreamTokenizer(br);
    Generic_StaticIO.setStreamTokenizerSyntax5(st);
    st.wordChars('(', '(');
    st.wordChars(')', ')');
    st.wordChars('%', '%');
    Generic_StaticIO.skipline(st);
    int token;
    String line = "";
    String[] fields;
    try {
        token = st.nextToken();
        int ID;
        //int pointID;
        while (token != StreamTokenizer.TT_EOF) {
            switch (token) {
            case StreamTokenizer.TT_EOL:
                //flowacc,area (km2),slope_25_(%),proglac_ID,COUNT
                //12.11111069,0.00756944,32.33880000000,0,250631
                fields = line.split(sComma);
                ID = Integer.valueOf(fields[3]);
                if (ID > 0) {
                    //BigDecimal flowacc;
                    BigDecimal area;
                    BigDecimal slope;
                    Object[] data;
                    BigDecimal maxx;
                    BigDecimal maxy;
                    BigDecimal minx;
                    BigDecimal miny;
                    data = result.get(ID);
                    ArrayList<Generic_XYNumericalData> theGeneric_XYNumericalData;
                    if (data == null) {
                        data = new Object[5];
                        theGeneric_XYNumericalData = new ArrayList<Generic_XYNumericalData>();
                        maxx = BigDecimal.ZERO;
                        maxy = BigDecimal.ZERO;
                        minx = BigDecimal.valueOf(Double.MAX_VALUE);
                        miny = BigDecimal.valueOf(Double.MAX_VALUE);
                        data[0] = theGeneric_XYNumericalData;
                        data[1] = maxx;
                        data[2] = minx;
                        data[3] = maxy;
                        data[4] = miny;
                        result.put(ID, data);
                    } else {
                        theGeneric_XYNumericalData = (ArrayList<Generic_XYNumericalData>) data[0];
                        maxx = (BigDecimal) data[1];
                        minx = (BigDecimal) data[2];
                        maxy = (BigDecimal) data[3];
                        miny = (BigDecimal) data[4];
                    }
                    //pointID = Integer.valueOf(fields[4]);
                    //flowacc = new BigDecimal(fields[0]);
                    area = new BigDecimal(fields[1]);
                    if (area.compareTo(BigDecimal.ZERO) == 1) {
                        area = Generic_BigDecimal.log(10, area, 10, RoundingMode.HALF_UP);
                    } else {
                        area = BigDecimal.ZERO;
                    }
                    slope = new BigDecimal(fields[2]);
                    if (slope.compareTo(BigDecimal.ZERO) == 1) {
                        slope = Generic_BigDecimal.log(10, slope, 10, RoundingMode.HALF_UP);
                    } else {
                        slope = BigDecimal.ZERO;
                    }
                    Generic_XYNumericalData point;
                    point = new Generic_XYNumericalData(slope, area);
                    theGeneric_XYNumericalData.add(point);
                    data[0] = theGeneric_XYNumericalData;
                    data[1] = maxx.max(slope);
                    data[2] = minx.min(slope);
                    data[3] = maxy.max(area);
                    data[4] = miny.min(area);
                }
                break;
            case StreamTokenizer.TT_WORD:
                line = st.sval;
                break;
            }
            token = st.nextToken();
        }
    } catch (IOException ex) {
        Logger.getLogger(SlopeAreaAnalysis.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:uk.ac.leeds.ccg.andyt.projects.fluvialglacial.SlopeAreaAnalysis.java

protected TreeMap<Integer, Object[]> readAustriaData(File fileIn) {
    TreeMap<Integer, Object[]> result;
    result = new TreeMap<Integer, Object[]>();
    BufferedReader br;// ww w .  j  av a2 s  . co m
    br = Generic_StaticIO.getBufferedReader(fileIn);
    StreamTokenizer st;
    st = new StreamTokenizer(br);
    Generic_StaticIO.setStreamTokenizerSyntax5(st);
    st.wordChars('(', '(');
    st.wordChars(')', ')');
    st.wordChars('%', '%');
    Generic_StaticIO.skipline(st);
    int token;
    String line = "";
    String[] fields;
    try {
        token = st.nextToken();
        int ID;
        //int pointID;
        while (token != StreamTokenizer.TT_EOF) {
            switch (token) {
            case StreamTokenizer.TT_EOL:
                //flowacc,area (km2),slope_25_(%),proglac_ID,COUNT
                //12.11111069,0.00756944,32.33880000000,0,250631
                fields = line.split(sComma);
                ID = Double.valueOf(fields[1]).intValue();
                if (ID > 0) {
                    //BigDecimal flowacc;
                    BigDecimal area;
                    BigDecimal slope;
                    Object[] data;
                    BigDecimal maxx;
                    BigDecimal maxy;
                    BigDecimal minx;
                    BigDecimal miny;
                    data = result.get(ID);
                    ArrayList<Generic_XYNumericalData> theGeneric_XYNumericalData;
                    if (data == null) {
                        data = new Object[5];
                        theGeneric_XYNumericalData = new ArrayList<Generic_XYNumericalData>();
                        maxx = BigDecimal.ZERO;
                        maxy = BigDecimal.ZERO;
                        minx = BigDecimal.valueOf(Double.MAX_VALUE);
                        miny = BigDecimal.valueOf(Double.MAX_VALUE);
                        data[0] = theGeneric_XYNumericalData;
                        data[1] = maxx;
                        data[2] = minx;
                        data[3] = maxy;
                        data[4] = miny;
                        result.put(ID, data);
                    } else {
                        theGeneric_XYNumericalData = (ArrayList<Generic_XYNumericalData>) data[0];
                        maxx = (BigDecimal) data[1];
                        minx = (BigDecimal) data[2];
                        maxy = (BigDecimal) data[3];
                        miny = (BigDecimal) data[4];
                    }
                    //pointID = Integer.valueOf(fields[4]);
                    //flowacc = new BigDecimal(fields[0]);
                    area = new BigDecimal(fields[3]);
                    if (area.compareTo(BigDecimal.ZERO) == 1) {
                        area = Generic_BigDecimal.log(10, area, 10, RoundingMode.HALF_UP);
                    } else {
                        area = BigDecimal.ZERO;
                    }
                    slope = new BigDecimal(fields[2]);
                    if (slope.compareTo(BigDecimal.ZERO) == 1) {
                        slope = Generic_BigDecimal.log(10, slope, 10, RoundingMode.HALF_UP);
                    } else {
                        slope = BigDecimal.ZERO;
                    }
                    Generic_XYNumericalData point;
                    point = new Generic_XYNumericalData(slope, area);
                    theGeneric_XYNumericalData.add(point);
                    data[0] = theGeneric_XYNumericalData;
                    data[1] = maxx.max(slope);
                    data[2] = minx.min(slope);
                    data[3] = maxy.max(area);
                    data[4] = miny.min(area);
                }
                break;
            case StreamTokenizer.TT_WORD:
                line = st.sval;
                break;
            }
            token = st.nextToken();
        }
    } catch (IOException ex) {
        Logger.getLogger(SlopeAreaAnalysis.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}