0

I am creating a webApp that will download some data from Yahoo Finance into a CSV file and then (hopefully) be able to then read the created CSV data into a HTML table.

I have successfully got the program to connect to the Yahoo feed and then download that data into a CSV file and would not like to use the data from the file into a table.

Below is the code i used to create the CSV file:

    String ticker = request.getParameter("stockSym");
    URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + ticker + "&f=abc");
    InputStream in = url.openStream();
    BufferedInputStream bufIn = new BufferedInputStream(in);

    File f=new File("stockInfo.csv");
    FileOutputStream fop=new FileOutputStream(f);

    for (;;)
    {
        int data = bufIn.read();

        // Check for EOF
        if (data == -1)
            break;
        else
            fop.write((char) data);
    }
    fop.flush();
    fop.close();

Are there any JSP programmers who would know how to open and then parse a CSV file into a table or would know of any good links to tutorials on how to accomplish this task?

Thanks.

Elliot Smith
  • 61
  • 1
  • 2
  • 3
  • 1
    The Ostermiller utilities have a CSV parser in them. Don't do it JSP however; that's crazy. Parse the CSV file as soon as you get it and turn it into something that's easy to work with from a page. – Pointy Oct 24 '10 at 23:49

2 Answers2

1

Do not write the CSV parsing code yourself, use one of the many available libraries.

They can handle tricky things like line breaks, quotes, embedded commas and so on.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

There is open source routine to parse CSV files here:

http://scm.opendap.org:8090/trac/browser/trunk/ODC/src/opendap/clients/odc/Utility.java

This utility class has a bunch a different functionality in it. Just pull out what is needed to parse the CSV.