Example: Space Separated Data

This example reads a set of stock prices in a space separated form.

The first few lines of the data set are as follows:

Date        Open    High    Low     Close   Volume
28-Apr-03   3.3     3.34    3.27    3.33    37224400
25-Apr-03   3.35    3.38    3.25    3.26    57117400
24-Apr-03   3.32    3.40    3.30    3.38    47019800
23-Apr-03   3.34    3.44    3.30    3.37    63243800
22-Apr-03   3.24    3.38    3.22    3.36    67392500

The first line contains the column names, with a comma as the separator. The rest of the lines contain data, one day per line. The first column is Date data and the last column is int data. All of the rest is double data. The data's class is set for each column. The parser is explicitly set for the date column, because it cannot be guessed by FlatFile . The date's locale is set to US , so that the example will work with a different default locale.

A Tokenizer is created and used that counts multiple separators (spaces) as one separator.

The class FlatFileEx2 extends com.imsl.io.FlatFile . The FlatFileEx2 constructor reads the line containing the column names, parses the names, and sets the column names.

The class FlatFileEx2 is used in the method main . The data set is assumed to be in a file called "SUNW.txt" in the same location as the example class file, so the getResourceAsStream method getResourceAsStream can be used to open the file as a stream. Some of the columns are printed out for each stock price.

import com.imsl.io.*;
import java.text.DateFormat;
import java.io.*;
import java.sql.SQLException;
import java.util.StringTokenizer;
import java.sql.Date;


public class FlatFileEx2 extends FlatFile {    
    static DateFormat dateFormat = DateFormat.getDateInstance();
    
    public FlatFileEx2(BufferedReader br, Tokenizer tokenizer) throws IOException {
        super(br, tokenizer);
        String line = readLine();
        StringTokenizer st = new StringTokenizer(line, " ", false);
        for (int j = 0;  st.hasMoreTokens();  j++) {
            setColumnName(j+1, st.nextToken().trim());
        }
        setColumnClass(1, Date.class); // Date
        setDateColumnParser(1, "dd-MMM-yy", java.util.Locale.US);
        setColumnClass(2, Double.class);  // Open
        setColumnClass(3, Double.class);  // High
        setColumnClass(4, Double.class);  // Low
        setColumnClass(5, Double.class);  // Close
        setColumnClass(6, Integer.class); // Volume
    }
 
    
    public static void main(String[] args) throws SQLException, IOException {
        InputStream is = FlatFileEx2.class.getResourceAsStream("SUNW.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Tokenizer tokenizer = new Tokenizer(" ", (char)0, true);
        FlatFileEx2 reader = new FlatFileEx2(br, tokenizer);

        while (reader.next()) {
            Date date = reader.getDate("Date");
            double close = reader.getDouble("Close");
            int volume = reader.getInt("Volume");
            System.out.println(dateFormat.format(date) + "  " + close + "  " + volume);
        }
    }
}

Output

Apr 28, 2003  3.33  37224400
Apr 25, 2003  3.26  57117400
Apr 24, 2003  3.38  47019800
Apr 23, 2003  3.37  63243800
Apr 22, 2003  3.36  67392500
Apr 21, 2003  3.28  58523800
Apr 17, 2003  3.24  101856900
Apr 16, 2003  3.32  54912900
Apr 15, 2003  3.35  33604200
Apr 14, 2003  3.29  38851800
Apr 11, 2003  3.31  38424000
Apr 10, 2003  3.37  38608500
Apr 9, 2003  3.28  50669700
Apr 8, 2003  3.31  46106400
Apr 7, 2003  3.36  47462900
Apr 4, 2003  3.34  48689900
Apr 3, 2003  3.48  38304400
Apr 2, 2003  3.49  48510200
Apr 1, 2003  3.36  38823800
Mar 31, 2003  3.26  38949300
Mar 28, 2003  3.42  27186700
Mar 27, 2003  3.56  40054700
Mar 26, 2003  3.5  30952400
Mar 25, 2003  3.45  63787600
Mar 24, 2003  3.45  34645400
Mar 21, 2003  3.72  53745900
Mar 20, 2003  3.65  47358500
Mar 19, 2003  3.57  51280600
Mar 18, 2003  3.55  51727400
Mar 17, 2003  3.53  69296600
Mar 14, 2003  3.24  59278900
Mar 13, 2003  3.31  58360700
Mar 12, 2003  3.08  71790300
Mar 11, 2003  3.21  42498400
Link to Java source.