CSV library for Java that is fast, RFC-compliant and dependency-free.
FastCSV is a lightning-fast, dependency-free CSV library for Java that conforms to RFC standards.
The primary use cases of FastCSV include:
[!NOTE] This selected benchmark is based on the Java CSV library benchmark suite
While maintaining high performance, FastCSV serves as a strict RFC 4180 CSV writer while also exhibiting the ability to read somewhat garbled CSV data. See JavaCsvComparison for details.
As one of the most popular CSV libraries for Java on GitHub, FastCSV comes with a wide range of features:
CRLF
(Windows), LF
(Unix) and CR
(old macOS)CRLF
, LF
, or CR
– can also be mixed)[!NOTE] Android is not Java and is not officially supported. Nevertheless, some basic checks are included in the continuous integration pipeline to verify that the library should work with Android.
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("header2")));
CsvCallbackHandler<NamedCsvRecord> callbackHandler =
new NamedCsvRecordHandler("header1", "header2");
CsvReader.builder().build(callbackHandler, "field 1,field 2")
.forEach(rec -> System.out.println(rec.getField("header2")));
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("header1", "header2")
.writeRecord("value1", "value2");
System.out.println(sw);
try (CsvWriter csv = CsvWriter.builder().build(file)) {
csv
.writeRecord("header1", "header2")
.writeRecord("value1", "value2");
}
CsvWriter.builder()
.fieldSeparator(',')
.quoteCharacter('"')
.quoteStrategy(QuoteStrategies.ALWAYS)
.commentCharacter('#')
.lineDelimiter(LineDelimiter.LF);
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.