FastCSV is a lightning-fast, dependency-free CSV library for Java that adheres to RFC standards.
Note
This selected benchmark is based on the Java CSV library benchmark suite
FastCSV maintains high performance while serving as a strict RFC 4180 CSV writer and can read somewhat garbled CSV data. See JavaCsvComparison for details.
The main features of FastCSV include:
- Enables ultra-fast reading and writing of CSV data
- Has zero runtime dependencies
- Maintains a small footprint
- Provides a null-free and developer-friendly API
- Compatible with GraalVM Native Image
- Delivered as an OSGi-compliant bundle
- Actively developed and maintained
- Well-tested and documented
- Crafted with natural intelligence, ❤️, and AI assistance ✨
- In big data applications: efficiently reading and writing data on a massive scale.
- In small data applications: serving as a lightweight library without additional dependencies.
- Compliant with RFC 4180 – including:
- Newline and field separator characters in fields
- Quote escaping
- Configurable field separator
- Supports line endings
CRLF
(Windows),LF
(Unix) andCR
(old macOS) - Supports unicode characters
- Supports reading of some non-compliant (real-world) data
- Preserves line break character(s) within fields
- Preserves the starting line number (even with skipped and multi-line records) – helpful for error messages
- Auto-detection of line delimiters (
CRLF
,LF
, orCR
– can also be mixed) - Configurable data validation
- Supports optional header records (access fields by name)
- Supports skipping empty lines
- Supports commented lines (skipping & reading) with configurable comment character
- Configurable field modifiers (e.g., to trim fields)
- Flexible callback handlers (e.g., to directly map to domain objects)
- BOM (Byte Order Mark) support (UTF-8, UTF-16 LE/BE, UTF-32 LE/BE)
- Supports flexible quoting strategies (e.g., to differentiate between empty and null)
- Supports writing comments
- for 3.x version: Java ⩾ 11 (Android 13 / API level 33)
- for 2.x version: Java ⩾ 8 (Android 8 / API level 26)
Note
Checks are included in the continuous integration pipeline to verify the library's functionality with Android. Nevertheless, the library is not tested on Android devices nor can I provide any support for Android-specific issues.
CsvReader.builder().ofCsvRecord("foo1,bar1\nfoo2,bar2")
.forEach(System.out::println);
try (CsvReader<CsvRecord> csv = CsvReader.builder().ofCsvRecord(file)) {
csv.forEach(System.out::println);
}
CsvReader.builder().ofNamedCsvRecord("header 1,header 2\nfield 1,field 2")
.forEach(rec -> System.out.println(rec.getField("header 2")));
CsvCallbackHandler<NamedCsvRecord> callbackHandler =
new NamedCsvRecordHandler("header 1", "header 2");
CsvReader.builder().build(callbackHandler, "field 1,field 2")
.forEach(rec -> System.out.println(rec.getField("header 2")));
CsvReader.builder()
.fieldSeparator(';')
.quoteCharacter('"')
.commentStrategy(CommentStrategy.SKIP)
.commentCharacter('#')
.skipEmptyLines(true)
.ignoreDifferentFieldCount(false)
.acceptCharsAfterQuotes(false)
.detectBomHeader(false);
try (IndexedCsvReader<CsvRecord> csv = IndexedCsvReader.builder().ofCsvRecord(file)) {
CsvIndex index = csv.getIndex();
System.out.println("Items of last page:");
int lastPage = index.getPageCount() - 1;
List<CsvRecord> csvRecords = csv.readPage(lastPage);
csvRecords.forEach(System.out::println);
}
var sw = new StringWriter();
CsvWriter.builder().build(sw)
.writeRecord("header 1", "header 2")
.writeRecord("value 1", "value 2");
System.out.println(sw);
try (CsvWriter csv = CsvWriter.builder().build(file)) {
csv
.writeRecord("header 1", "header 2")
.writeRecord("value 1", "value 2");
}
CsvWriter.builder()
.fieldSeparator(',')
.quoteCharacter('"')
.quoteStrategy(QuoteStrategies.ALWAYS)
.commentCharacter('#')
.lineDelimiter(LineDelimiter.LF);
- Examples
- JavaDoc
- How to Upgrade
- Changelog
- Design & Architecture
- CSV Interpretation
- Design Goals
- How to Contribute
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.