Skip to content

nbbrd/picocsv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

picocsv - lightweight CSV library for Java

Download

This Java library handles CSV content.
While directly usable, it is designed to be the core foundation of other libraries.

Key points:

  • lightweight library with no dependency
  • fast and efficient (no heap memory allocation)
  • designed to be embedded into other libraries as an external dependency or as a single-file source
  • has a module-info that makes it compatible with JPMS
  • Java 8 minimum requirement

Features:

  • reads/writes CSV from/to character streams
  • provides a minimalist low-level API
  • does not interpret content
  • does not correct invalid files
  • follows the RFC4180 specification
  • supports custom line separator
  • supports custom comment character

⚠️ Note that the Format#acceptMissingField option must be set to false to closely follow the RFC4180 specification. The default value is currently true but will be reversed in the next major release.

Examples

Read examples

Basic reading of all fields skipping comments:

try (java.io.Reader chars = ...; 
        Csv.Reader csv = Csv.Reader.of(Csv.Format.DEFAULT, Csv.ReaderOptions.DEFAULT, chars)) {
  while (csv.readLine()) {
    if (!csv.isComment()) {
      while (csv.readField()) {
        CharSequence field = csv;
        ...
      }
    }
  }
}

Configuring reading options:

Csv.ReaderOptions strict = Csv.ReaderOptions.builder().lenientSeparator(false).build();

Write examples

Basic writing of some fields and comments:

try (java.io.Writer chars = ...;
        Csv.Writer csv = Csv.Writer.of(Csv.Format.DEFAULT, Csv.WriterOptions.DEFAULT, chars)) {
  csv.writeComment("Some comment");
  csv.writeField("Some field");
  csv.writeEndOfLine();
}

Custom format

Csv.Format tsv = Csv.Format.builder().delimiter('\t').build();

Readable/Appendable

picocsv only supports java.io.Reader/java.io.Writer as input/output for performance reasons. However, it is still possible to use Readable/Appendable by wrapping them in adapters. See Cookbook#asCharReader(Readable) and Cookbook#asCharWriter(Appendable).

Setup

Maven setup:

<dependency>
    <groupId>com.github.nbbrd.picocsv</groupId>
    <artifactId>picocsv</artifactId>
    <version>LATEST_VERSION</version>
</dependency>

Developing

This project is written in Java and uses Apache Maven as a build tool.
It requires Java 8 as minimum version and all its dependencies are hosted on Maven Central.

The code can be build using any IDE or by just type-in the following commands in a terminal:

git clone https://github.com/nbbrd/picocsv.git
cd picocsv
mvn clean install

Contributing

Any contribution is welcome and should be done through pull requests and/or issues.

Licensing

The code of this project is licensed under the European Union Public Licence (EUPL).