Skip to content
Arnaud Roger edited this page Jul 5, 2015 · 19 revisions

Quick Start

Java 8

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .skip(2)
        .stream(reader)
        .map(Arrays::toString)
        .forEach(System.out::println);
}

Java 7

try (FileReader reader : new FileReader(myfile)) {
    Iterator<String[]> it = CsvParser.skip(2).iterate(reader);
    while(it.hasNext()) {
        System.out.println(Arrays.toString(it.next()));
    }
}

Java 6

FileReader reader =  new FileReader(myfile);
try {
    Iterator<String[]> it = CsvParser.skip(2).iterator(reader);
    while (it.hasNext()) {
        System.out.println(Arrays.toString(it.next()));
    }
} finally {
    input.close();
}

Customised separator

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .stream(reader)
        .map(Arrays::toString)
        .forEach(System.out::println);
}

With CsvMapper

Map to object

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .mapTo(MyObject.class)
        .stream(reader)
        .forEach(System.out::println);
}

Map to tuple

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .mapTo(String.class, Date.class, MyObject.class)
        .stream(reader)
        .forEach(System.out::println);
}

Map with mapper

CsvMapper mapper = ...;

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .mapWith(mapper)
        .stream(reader)
        .forEach(System.out::println);
}

specify headers manually

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .mapTo(String.class, Date.class, MyObject.class)
        .headers("elt0", "elt1", "elt2")
        .stream(reader)
        .forEach(System.out::println);
}

override file headers

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .separator('\t').quote('\'')
        .mapTo(String.class, Date.class, MyObject.class)
        .overrideHeaders("elt0", "elt1", "elt2")
        .stream(reader)
        .forEach(System.out::println);
}

customised date format

to change the date format you will need to do that at the mapper level hence use the mapWith function.

You can either change the default date format

CsvMapper<MyObject> mapper =
    CsvMapperFactory
        .newInstance()
        .defaultDateFormat("yyyyMMdd")
        .newMapper(MyObject.class);

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .mapWith(mapper)
        .stream(reader)
        .forEach(System.out::println);
}

Or override the format for a specific field.

CsvMapper<MyObject> mapper = 
    CsvMapperFactory
        .newInstance()
        .addColumnDefinition("my_date_column", CsvColumnDefinition.dateFormatDefinition("yyyyMMdd"))
        .newMapper(MyObject.class);

try (FileReader reader : new FileReader(myfile)) {
    CsvParser
        .mapWith(mapper)
        .stream(reader)
        .forEach(System.out::println);
}
Clone this wiki locally