The Artima Developer Community
Sponsored Link

Java Buzz Forum
Simple CSV

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Simple CSV Posted: May 24, 2005 8:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Simple CSV
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

I've been bitten a number of times in the last several months by there not being a decent CSV library in Java. CSV is just enough of a pain to want a somewhat thought out tool (escaping, quoted fields, possibly comment rows).

So, a simple csv tool that basically mirrors the ruby CSV library ;-) It just has a reader and writer, and usage is (hopefully) dirt easy enough that the docs consist of javadocs and some sample code in test cases:

given a sample CSV file:

Brian McCallister,(302) 994-8629
Eric McCallister,(302) 994-8991
Keith McCallister,(302) 994-8761

You can chunk it up as follows:

public void testExample1() throws Exception
{
    CSVReader reader = new SimpleReader();

    URL url = this.getClass().getClassLoader().getResource("sample.csv");
    InputStream in = url.openStream();

    List items = reader.parse(in);
    String[] first = (String[]) items.get(0);
    assertEquals("Brian McCallister", first[0]);
    assertEquals("(302) 994-8629", first[1]);

    String[] second = (String[]) items.get(1);
    assertEquals("Eric McCallister", second[0]);
    assertEquals("(302) 994-8991", second[1]);

    String[] third = (String[]) items.get(2);
    assertEquals("Keith McCallister", third[0]);
    assertEquals("(302) 994-8761", third[1]);

    in.close();
}

Or you can use the callback-based streaming api (for big files, for example):

public void testExample2() throws Exception
{
    CSVReader reader = new SimpleReader();

    URL url = this.getClass().getClassLoader().getResource("sample.csv");
    InputStream in = url.openStream();

    final int[] count = {0};
    reader.parse(in, new ReaderCallback()
    {
        public void onRow(String[] fields)
        {
            count[0]++;
            switch (count[0])
            {
                case 1:
                    assertEquals("Brian McCallister", fields[0]);
                    assertEquals("(302) 994-8629", fields[1]);
                    break;
                case 2:
                    assertEquals("Eric McCallister", fields[0]);
                    assertEquals("(302) 994-8991", fields[1]);
                    break;
                case 3:
                    assertEquals("Keith McCallister", fields[0]);
                    assertEquals("(302) 994-8761", fields[1]);
                    break;
            }
        }
    });
    assertEquals(3, count[0]);
    in.close();
}

Anyway, is trivially small, but does escaping, quotes, etc correctly -- including escaping for you when you use the writer. Have fun with it! There are no dependencies beyond the JRE standard library.

Read: Simple CSV

Topic: Java Problems in Mac OS X 10.3.9 Previous Topic   Next Topic Topic: Everybody loves Command-Space

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use