Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Simple CSV-Parser for Java
https://github.com/timo-reymann/csv-parser

csv csv-parser java java8 java9

Last synced: 4 months ago
JSON representation

Simple CSV-Parser for Java

Awesome Lists containing this project

README

        

# CSV-Parser
[![Maven Central Version](https://maven-badges.herokuapp.com/maven-central/com.github.timo-reymann/csv-parser/badge.svg)](https://search.maven.org/search?q=g:com.github.timo-reymann%20AND%20a:csv-parser&core=gav)
[![Build Status](https://travis-ci.org/timo-reymann/csv-parser.svg?branch=master)](https://travis-ci.org/timo-reymann/csv-parser)

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

```xml

com.github.timo-reymann
csv-parser
4.0.0

```

## 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
```java
@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
```java
@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

````java
CsvWriter writer = new CsvWriter.Builder()
.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

```java
CsvReader reader = new CsvReader.Builder()
.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](https://www.javadoc.io/doc/com.github.timo-reymann/csv-parser/)

## Need further information?
Just send me a mail :)

## Found a bug?
[Create a issue](https://github.com/timo-reymann/csv-parser/issues/new)