-
Notifications
You must be signed in to change notification settings - Fork 76
CsvParser
Arnaud Roger edited this page Jul 5, 2015
·
19 revisions
try (FileReader reader : new FileReader(myfile)) {
CsvParser
.skip(2)
.stream(reader)
.map(Arrays::toString)
.forEach(System.out::println);
}
try (FileReader reader : new FileReader(myfile)) {
Iterator<String[]> it = CsvParser.skip(2).iterate(reader);
while(it.hasNext()) {
System.out.println(Arrays.toString(it.next()));
}
}
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();
}
try (FileReader reader : new FileReader(myfile)) {
CsvParser
.separator('\t').quote('\'')
.stream(reader)
.map(Arrays::toString)
.forEach(System.out::println);
}
try (FileReader reader : new FileReader(myfile)) {
CsvParser
.separator('\t').quote('\'')
.mapTo(MyObject.class)
.stream(reader)
.forEach(System.out::println);
}
try (FileReader reader : new FileReader(myfile)) {
CsvParser
.separator('\t').quote('\'')
.mapTo(String.class, Date.class, MyObject.class)
.stream(reader)
.forEach(System.out::println);
}
CsvMapper mapper = ...;
try (FileReader reader : new FileReader(myfile)) {
CsvParser
.separator('\t').quote('\'')
.mapWith(mapper)
.stream(reader)
.forEach(System.out::println);
}
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);
}
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);
}
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);
}