package com.imsl.test.example.io; import com.imsl.io.*; import java.text.DateFormat; import java.io.*; import java.sql.*; import java.util.StringTokenizer; /** *

Reads in a data set in a space separated form.

* * 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. A data type 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. *

*

* This example extends the class {@link 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 that the * getResourceAsStream method can be used to open the file as a * stream. Some of the columns are printed out for each stock price.

* * @see Code * @see Output * */ 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); } reader.close(); br.close(); is.close(); } }