CSV Tools

Utility classes to simply reading from, and writing to, CSV (or other character delimited) files. They provide for splitting up, and building, flat files into arrays or Strings. Very basic, but they handle escaping, quotes, etc.

Reader Basic Usage

Given the simple input file:

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

Here is some code...


    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();
    }

    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();
    }
    

Writer Basic Usage:

    public void testExample1() throws Exception
    {
        StringWriter buffer = new StringWriter();
        CSVWriter writer = new SimpleWriter(buffer);

        writer.append(new Object[]{"brian", "1"});
        writer.rawLine("# some comment");
        writer.append(new Object[]{"eric", "2"});
        String file = buffer.getBuffer().toString();

        assertEquals("brian,1\n# some comment\neric,2\n", file);
    }

    public void testCallback() throws Exception
    {
        File temp = File.createTempFile("test", ".csv");
        temp.deleteOnExit();

        SimpleWriter.write(temp, new WriterCallback()
        {
            public void withWriter(SimpleWriter writer) throws Exception
            {
                writer.append(new Object[]{"brian", "1"});
            }
        });

        List lines = new SimpleReader().parse(temp);
        assertEquals(1, lines.size());
        String[] fields = (String[]) lines.get(0);
        assertEquals("brian", fields[0]);
        assertEquals("1", fields[1]);
    }
    

Copyright (c) 2005, Brian McCallister