csv-parser

Simple CSV-Parser

License

License

MIT
Categories

Categories

CSV Data Data Formats
GroupId

GroupId

com.github.timo-reymann
ArtifactId

ArtifactId

csv-parser
Last Version

Last Version

4.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

csv-parser
Simple CSV-Parser
Project URL

Project URL

https://github.com/timo-reymann/csv-parser
Source Code Management

Source Code Management

https://github.com/timo-reymann/csv-parser/tree/master

Download csv-parser

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.timo-reymann/csv-parser/ -->
<dependency>
    <groupId>com.github.timo-reymann</groupId>
    <artifactId>csv-parser</artifactId>
    <version>4.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.timo-reymann/csv-parser/
implementation 'com.github.timo-reymann:csv-parser:4.0.0'
// https://jarcasting.com/artifacts/com.github.timo-reymann/csv-parser/
implementation ("com.github.timo-reymann:csv-parser:4.0.0")
'com.github.timo-reymann:csv-parser:jar:4.0.0'
<dependency org="com.github.timo-reymann" name="csv-parser" rev="4.0.0">
  <artifact name="csv-parser" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.timo-reymann', module='csv-parser', version='4.0.0')
)
libraryDependencies += "com.github.timo-reymann" % "csv-parser" % "4.0.0"
[com.github.timo-reymann/csv-parser "4.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.mapstruct : mapstruct-processor jar 1.3.0.Final

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.12

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.6.0
org.junit.jupiter : junit-jupiter-engine jar 5.6.0

Project Modules

There are no modules declared in this project.

CSV-Parser

Maven Central Version Build Status

Parse csv files and other seperated values using java.

Limitations

Currently all primitive types are suppported, plus LocalDate and LocalDateTime.

To use primitive types you must use the boxed types.

How to use?

Add to your depenencies

<dependency>
    <groupId>com.github.timo-reymann</groupId>
    <artifactId>csv-parser</artifactId>
    <version>4.0.0</version>
</dependency>

Create your bean class

Please keep in mind that you need an zero-args constructor for this parser to work properly!

... using the index for mapping

@Data
public class MyBean {
    @CsvColumn(index = 0)
    private Integer id;

    @CsvColumn(index = 1)
    private String firstName;

    @CsvColumn(index = 2)
    private String lastName;

    @CsvColumn(index = 3)
    private String email;

    @CsvColumn(index = 4)
    private String gender;

    @CsvColumn(index = 5)
    private String ip;
}

... using the heading for mapping

@Data
class MyBean {
    @CsvColumn(headerName="id")
    private Integer id;

    @CsvColumn(headerName="first_name")
    private String firstName;

    @CsvColumn(headerName="last_name")
    private String lastName;

    @CsvColumn(headerName="email")
    private String email;

    @CsvColumn(headerName="gender")
    private String gender;

    @CsvColumn(headerName="ip_address")
    private String ip;
}

Write csv file

CsvWriter<MyBean> writer = new CsvWriter.Builder<MyBean>()
             .forClass(MyBean.class)            // entity class
             .file(new File("customers.csv"))   // file
             .outputStream(myInputStream)        // or even stream, but ATTENTION: an OutputStream will always be overwritten
             .noAppend()                        // replace file every time
             .build();

// Create bean and set values
MyBean myBean = new MyBean();
myBean.setId(1);
myBean.setFirstName("Foo");
myBean.setLastName("Bar");
myBean.setEmail("[email protected]");
myBean.setGender("christmasTree");
myBean.setIp("127.0.0.1");

// Map object and add to file buffer
writer.writeLine(myBean);

// Write changes to disk
writer.close();

Read csv file

CsvReader<MyBean> reader = new CsvReader.Builder<MyBean>()
                .forClass(MyBean.class)         // bean class object
                .file(new File("test.csv"))     // specify file
                .inputStream(myInputStream)        // or even stream
                .hasHeading()                   // file has headings
                .build();

// Read all lines and print to console
reader.lines().forEach(System.out::println);

Supported java versions

The parser is compatible with Java 11+.

If you need support for java 8 you must use version <= 3.1.0

There are only two things for you to do:

  1. Add to your module: requires com.github.timo_reymann.csv_parser
  2. Open your package for reflection containing bean classes like this: opens my.entities to com.github.timo_reymann.csv_parser

Thats it!

JavaDoc

If you are looking for the JavaDoc, its here

Need further information?

Just send me a mail :)

Found a bug?

Create a issue

Versions

Version
4.0.0
3.1.0
3.0.0
2.4.0
2.3.0
2.2.1
2.1.1
2.1.0
2.0.0
1.4.0
1.3.0
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.4
1.0.2
1.0.1
1.0