sigpwned / csv4j

Simple CSV reading and writing for Java 8+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CSV4J tests Security Rating Maintainability Rating Reliability Rating Maven Central Version

CSV4J is a simple CSV reader and writer for Java 8 or later.

Goals

  • Provide CSV reading and writing functionality compatible with both de-facto and official standards
  • Expose low-level encoding data
  • Simple, compact API

Non-Goals

  • Object mapping
  • Header handling

Why Yet Another CSV Library?

There are already many good libraries with CSV support available: Apache Commons CSV, Super CSV, Opencsv, Jackson, etc. So why publish another?

In my experience, most of these libraries either:

  1. Have long-standing bugs
  2. Are complex to use
  3. Do too much
  4. Do not give visibility into low-level encoding data, particularly whether individual fields are quoted

In response, this library is designed to be:

  1. Correct
  2. Simple
  3. Focused
  4. Transparent

Code Examples

To read data in the standard CSV format, use:

try (CsvReader rows=new CsvReader(openReader())) {
    for(CsvRecord row=rows.readNext();row!=null;row=rows.readNext()) {
        // Do something here
    }
}

To write data in the standard CSV format, use:

try (CsvWriter rows=new CsvWriter(openWriter())) {
    rows.writeNext(CsvRecord.of(
        CsvField.of(true, "Hello"),
        CsvField.of(true, "World")));
    rows.writeNext(CsvRecord.of(
        CsvField.of(false, "Foo"),
        CsvField.of(false, "Bar")));
}

The CsvReader also has an iterator capability:

try (CsvReader rows=new CsvReader(openReader())) {
    for(CsvRecord row : rows) {
        // Do something here
    }
}

The CsvReader also has a stream capability:

try (CsvReader rows=new CsvReader(openReader())) {
    rows.stream.forEach(r -> {
        // Do something here
    });
}

Related projects

The csv4j library has no dependencies. However, these libraries may be useful when processing CSV data.

Users may find chardet4j useful for decoding byte streams into character streams when character encodings are not known ahead of time, for example with user input:

try (CsvReader rows=new CsvReader(Chardet.decode(openInputStream(), StandardCharsets.UTF_8))) {
    // Process rows here like normal...
}

About

Simple CSV reading and writing for Java 8+

License:Apache License 2.0


Languages

Language:Java 100.0%