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 jdbcMapper.
CsvMapper<MyClass> jdbcMapper = CsvMapperFactory.newInstance().newMapper(MyClass.class);
CsvParser.mapWith(jdbcMapper).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);
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCsvParser.AbstractDSL<D extends CsvParser.AbstractDSL<D>>static final classDSL for csv parsing.static final classstatic interfaceCsvParser.OnReaderFactory<T,D extends CsvParser.AbstractDSL<?>> -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CsvParser.DSLbufferSize(int size) static CsvParser.DSLdsl()static <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>>
Hstatic <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>>
Hstatic <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>>
HforEach(CharSequence content, H consumer) static org.simpleflatmapper.util.CloseableIterator<String[]>iterator(CharSequence content) static CsvParser.DSLlimit(int limit) static CsvParser.DSLmaxBufferSize(int size) static <R,D extends CsvParser.AbstractDSL<?>>
RonReader(File file, D dsl, CsvParser.OnReaderFactory<R, ? super D> factory) static <CC extends CellConsumer>
CCstatic <CC extends CellConsumer>
CCstatic <CC extends CellConsumer>
CCparse(CharSequence content, CC cellConsumer) static <CC extends CellConsumer>
CCstatic CsvParser.DSLquote(char c) static CloseableCsvReaderstatic CsvReaderstatic CsvReaderreader(CharSequence content) static CsvReaderstatic CsvParser.DSLseparator(char c) static CsvParser.DSLskip(int skip) Deprecated.static <R> R
-
Field Details
-
DEFAULT_MAX_BUFFER_SIZE_8M
public static final int DEFAULT_MAX_BUFFER_SIZE_8M- See Also:
-
DEFAULT_BUFFER_SIZE_4K
public static final int DEFAULT_BUFFER_SIZE_4K- See Also:
-
DEFAULT_CHARSET
-
-
Constructor Details
-
CsvParser
public CsvParser()
-
-
Method Details
-
separator
- Parameters:
c- the separator char- Returns:
- the DSL object
-
bufferSize
-
maxBufferSize
-
quote
-
skip
-
dsl
-
limit
-
reader
- Parameters:
reader- the reader- Returns:
- a csv reader based on the default setup.
- Throws:
IOException- if an error occurs reading the data
-
reader
- Throws:
IOException
-
reader
- Throws:
IOException
-
reader
- Throws:
IOException
-
iterator
- Throws:
IOException
-
iterator
- Throws:
IOException
-
iterator
public static org.simpleflatmapper.util.CloseableIterator<String[]> iterator(File file) throws IOException - Throws:
IOException
-
forEach
public static <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>> H forEach(Reader reader, H consumer) throws IOException - Throws:
IOException
-
forEach
public static <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>> H forEach(CharSequence content, H consumer) throws IOException - Throws:
IOException
-
forEach
public static <H extends org.simpleflatmapper.util.CheckedConsumer<String[]>> H forEach(File file, H consumer) throws IOException - Throws:
IOException
-
parse
- Throws:
IOException
-
parse
public static <CC extends CellConsumer> CC parse(CharSequence content, CC cellConsumer) throws IOException - Throws:
IOException
-
parse
public static <CC extends CellConsumer> CC parse(String content, CC cellConsumer) throws IOException - Throws:
IOException
-
parse
- Throws:
IOException
-
stream
- Throws:
IOException
-
stream
Deprecated.- Throws:
IOException
-
stream
- Throws:
IOException
-
stream
- Throws:
IOException
-
onReader
public static <R,D extends CsvParser.AbstractDSL<?>> R onReader(File file, D dsl, CsvParser.OnReaderFactory<R, ? super D> factory) throws IOException- Throws:
IOException
-