Popularity
4.7
Growing
Activity
9.0
Growing
556
15
94

Programming language: Java
License: MIT License
Tags: CSV     Projects    
Latest version: v2.1.0
Add another 'CSV' Library

README

build Codacy Badge codecov javadoc Maven Central

FastCSV is an ultra-fast and dependency-free RFC 4180 compliant CSV library for Java.

Actively developed and maintained since 2015 its primary intended use cases are:

  • big data applications to read and write data on a massive scale
  • small data applications with the need for a lightweight library

Benchmark & Compatibility

A selected benchmark from the Java CSV library benchmark suite project:

[Benchmark](benchmark.png "Benchmark")

While maintaining high performance, FastCSV is a strict RFC 4180 CSV writer but also able to read garbled CSV data (to some degree). See JavaCsvComparison for details.

Features

API

  • Ultra fast
  • Small footprint
  • Zero runtime dependencies
  • Null-free

CSV specific

  • RFC 4180 compliant – including:
    • Newline and field separator characters in fields
    • Quote escaping
  • Configurable field separator
  • Support for line endings CRLF (Windows), CR (old macOS) and LF (Unix)
  • Unicode support

Reader specific

  • Support reading of some non-compliant (real world) data
  • Preserving line break character(s) within fields
  • Preserving the original line number (even with skipped and multi line records) – helpful for error messages
  • Auto-detection of line delimiters (can also be mixed)
  • Configurable data validation
  • Support for (optional) header lines (get field based on column name)
  • Support for skipping empty rows
  • Support for commented lines (skipping & reading) and configurable comment character
  • Support for random access file operations

Writer specific

  • Support for multiple quote strategies to differentiate between empty and null
  • Support for writing comments with proper quotation if needed

Requirements

  • Java 8

:bulb: Android is not Java and is not officially supported. Although some basic checks are included in the continuous integration pipeline in order to verify that the library should work with Android 8.0 (API level 26).

CsvReader Examples

Iterative reading of some CSV data from a string

CsvReader.builder().build("foo1,bar1\r\nfoo2,bar2")
    .forEach(System.out::println);

Iterative reading of some CSV data with a header

NamedCsvReader.builder().build("header 1,header 2\nfield 1,field 2")
    .forEach(row -> row.getField("header 2"));

Iterative reading of a CSV file

try (CsvReader csv = CsvReader.builder().build(path)) {
    csv.forEach(System.out::println);
}

Custom settings

CsvReader.builder()
    .fieldSeparator(';')
    .quoteCharacter('"')
    .commentStrategy(CommentStrategy.SKIP)
    .commentCharacter('#')
    .skipEmptyRows(true)
    .errorOnDifferentFieldCount(false);

For more example see [CsvReaderExample.java](src/example/java/example/CsvReaderExample.java)

CsvWriter Examples

Iterative writing of some data to a writer

CsvWriter.builder().build(new PrintWriter(System.out, true))
    .writeRow("header1", "header2")
    .writeRow("value1", "value2");

Iterative writing of a CSV file

try (CsvWriter csv = CsvWriter.builder().build(path)) {
    csv
        .writeRow("header1", "header2")
        .writeRow("value1", "value2");
}

Custom settings

CsvWriter.builder()
    .fieldSeparator(',')
    .quoteCharacter('"')
    .quoteStrategy(QuoteStrategy.REQUIRED)
    .lineDelimiter(LineDelimiter.LF);

For more example see [CsvWriterExample.java](src/example/java/example/CsvWriterExample.java)

Upgrading from version 1.x

Please see [UPGRADING.md](UPGRADING.md) for an overview of the main functionality of 1.x and how to upgrade them to version 2.


Sponsoring and partnerships

YourKit

YourKit was used to optimize the performance and footprint of FastCSV. YourKit is the creator of YourKit Java Profiler, YourKit .NET Profiler, and YourKit YouMonitor.