Example #1
0
  /**
   * Create a gold price object from a correctly formatted csv file
   *
   * @param csvFile The csv file downloaded from yahoo
   * @throws IOException
   */
  private void parseCsvFile(String csvFile) throws IOException {

    // read file
    byte[] data = com.tools.Tools.readFile(csvFile);
    if (data == null) throw new IOException("File could not be read");
    String string = new String(data, CHARSET);

    // split by commas
    ArrayList<String> items = new ArrayList<String>(Arrays.asList(string.split("\\s*,\\s*")));

    // make sure has 4 items
    if (items.size() != N_FIELDS)
      throw new IOException("File is not formatted correctly. Expect 4 fields");

    // check ticker
    if (!items.get(0).equals(getFullTicker()))
      throw new IOException("Ticker symbol is not correct. Received " + items.get(0));

    // strip off end of line
    items.set(3, items.get(3).replaceAll("\\r\\n", ""));

    // create date
    SimpleDateFormat format = new SimpleDateFormat("\"MM/dd/yyyy\"\"hh:mma\"", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
    try {
      date = format.parse(items.get(2) + items.get(3));
    } catch (ParseException e) {
      throw new IOException("Bad file format: " + items.get(2) + items.get(3));
    }

    // grab price
    price = Double.parseDouble(items.get(1));
  }