public final class CsvParser extends Object
It is possible to customize the quote char, the separator, skip lines,and specified the size of the buffer
CsvParser
.quote('\'')
.separator(';')
.skip(2)
.bufferSize(256)
.stream(new StringReader("1;1\n2;2\n3;3\n4;4\n5;5"))
.map(Arrays::toString)
.forEach(System.out::println);
// output
// [3, 3]
// [4, 4]
// [5, 5]
the limit settings does not affect streams or iterator, only parse on DSL and forEach on the mapTo/mapWith DSL.
The DSL provides way to mapTo an object
CsvParser.mapTo(MyClass.class).stream(reader).forEach(System.out::println);
using static mapping when no headers
CsvParser.mapTo(MyClass.class).addHeaders("id", "field").stream(reader).forEach(System.out::println);
// using the addMapping
CsvParser.mapTo(MyClass.class).addMapping("id").addMapping("field").stream(reader).forEach(System.out::println);
using static mapping and ignoring source header
CsvParser.mapTo(MyClass.class).overrideHeaders("id", "field").stream(reader).forEach(System.out::println);
// using the addMapping
CsvParser.skip(1).mapTo(MyClass.class).addMapping("id").addMapping("field").stream(reader).forEach(System.out::println);
or mapping with a predefined mapper.
CsvMapper<MyClass> mapper = CsvMapperFactory.newInstance().newMapper(MyClass.class);
CsvParser.mapWith(mapper).stream(reader).forEach(System.out::println);
Each call to the DSL return an immutable representation of the current setup. So that it is possible to cache step of the DSL without side effect.
CsvParser.DSL semiColumnParser = CsvParser.separator(';');
try (Reader r = new FileReader(file)) {
// the limit does not modify to the semiColumnParser object
semiColumnParser.limit(3).stream(r);
}
| Modifier and Type | Class and Description |
|---|---|
static class |
CsvParser.DSL
DSL for csv parsing.
|
static class |
CsvParser.MapToDSL<T>
DSL for csv mapping to a dynamic mapper.
|
static class |
CsvParser.MapWithDSL<T>
DSL for csv mapping to a provided mapper.
|
static class |
CsvParser.StaticMapToDSL<T>
DSL for csv mapping to a static mapper.
|
| Constructor and Description |
|---|
CsvParser() |
| Modifier and Type | Method and Description |
|---|---|
static CsvParser.DSL |
bufferSize(int size) |
static Iterator<String[]> |
iterate(Reader reader)
Deprecated.
|
static Iterator<String[]> |
iterator(Reader reader) |
static CsvParser.DSL |
limit(int limit) |
static <T> CsvParser.MapToDSL<T> |
mapTo(Class<T> type) |
static <T1,T2> CsvParser.MapToDSL<Tuple2<T1,T2>> |
mapTo(Class<T1> class1,
Class<T2> class2) |
static <T1,T2,T3> CsvParser.MapToDSL<Tuple3<T1,T2,T3>> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3) |
static <T1,T2,T3,T4> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3,
Class<T4> class4) |
static <T1,T2,T3,T4,T5> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3,
Class<T4> class4,
Class<T5> class5) |
static <T1,T2,T3,T4,T5,T6> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3,
Class<T4> class4,
Class<T5> class5,
Class<T6> class6) |
static <T1,T2,T3,T4,T5,T6,T7> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3,
Class<T4> class4,
Class<T5> class5,
Class<T6> class6,
Class<T7> class7) |
static <T1,T2,T3,T4,T5,T6,T7,T8> |
mapTo(Class<T1> class1,
Class<T2> class2,
Class<T3> class3,
Class<T4> class4,
Class<T5> class5,
Class<T6> class6,
Class<T7> class7,
Class<T8> class8) |
static <T> CsvParser.MapToDSL<T> |
mapTo(Type type) |
static <T> CsvParser.MapToDSL<T> |
mapTo(TypeReference<T> type) |
static <T> CsvParser.MapWithDSL<T> |
mapWith(CsvMapper<T> mapper) |
static <CC extends CellConsumer> |
parse(Reader reader,
CC cellConsumer) |
static CsvParser.DSL |
quote(char c) |
static CsvReader |
reader(Reader reader) |
static CsvParser.DSL |
separator(char c) |
static CsvParser.DSL |
skip(int skip) |
public static CsvParser.DSL separator(char c)
c - the separator charpublic static CsvParser.DSL bufferSize(int size)
public static CsvParser.DSL quote(char c)
public static CsvParser.DSL skip(int skip)
public static CsvParser.DSL limit(int limit)
public static <T> CsvParser.MapToDSL<T> mapTo(Type type)
public static <T> CsvParser.MapToDSL<T> mapTo(Class<T> type)
public static <T> CsvParser.MapToDSL<T> mapTo(TypeReference<T> type)
public static <T1,T2> CsvParser.MapToDSL<Tuple2<T1,T2>> mapTo(Class<T1> class1, Class<T2> class2)
public static <T1,T2,T3> CsvParser.MapToDSL<Tuple3<T1,T2,T3>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3)
public static <T1,T2,T3,T4> CsvParser.MapToDSL<Tuple4<T1,T2,T3,T4>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3, Class<T4> class4)
public static <T1,T2,T3,T4,T5> CsvParser.MapToDSL<Tuple5<T1,T2,T3,T4,T5>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3, Class<T4> class4, Class<T5> class5)
public static <T1,T2,T3,T4,T5,T6> CsvParser.MapToDSL<Tuple6<T1,T2,T3,T4,T5,T6>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3, Class<T4> class4, Class<T5> class5, Class<T6> class6)
public static <T1,T2,T3,T4,T5,T6,T7> CsvParser.MapToDSL<Tuple7<T1,T2,T3,T4,T5,T6,T7>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3, Class<T4> class4, Class<T5> class5, Class<T6> class6, Class<T7> class7)
public static <T1,T2,T3,T4,T5,T6,T7,T8> CsvParser.MapToDSL<Tuple8<T1,T2,T3,T4,T5,T6,T7,T8>> mapTo(Class<T1> class1, Class<T2> class2, Class<T3> class3, Class<T4> class4, Class<T5> class5, Class<T6> class6, Class<T7> class7, Class<T8> class8)
public static <T> CsvParser.MapWithDSL<T> mapWith(CsvMapper<T> mapper)
public static CsvReader reader(Reader reader) throws IOException
reader - the readerIOException - if an error occurs reading the data@Deprecated public static Iterator<String[]> iterate(Reader reader) throws IOException
IOExceptionpublic static Iterator<String[]> iterator(Reader reader) throws IOException
IOExceptionpublic static <CC extends CellConsumer> CC parse(Reader reader, CC cellConsumer) throws IOException
IOExceptionCopyright © 2015. All rights reserved.