Code Monkey home page Code Monkey logo

fastcsv's Introduction

FastCSV

Build Status Maven Central

FastCSV is a ultra fast and simple RFC 4180 compliant CSV library for Java, licensed under the Apache License, Version 2.0.

Benchmark

Benchmark from the Java CSV library benchmark suite project:

Benchmark

Latest release

The most recent release is 1.0.2, released February 3, 2018.

To add a dependency using Maven, use the following:

<dependency>
    <groupId>de.siegmar</groupId>
    <artifactId>fastcsv</artifactId>
    <version>1.0.2</version>
</dependency>

To add a dependency using Gradle:

dependencies {
    compile 'de.siegmar:fastcsv:1.0.2'
}

Features

  • RFC 4180 compliant CSV reader and writer
  • Ultra fast
  • Configurable field separator
  • Configurable text delimiter
  • Support for line endings CRLF (Windows), CR (old Mac OS) and LF (Unix)
  • Support for (optional) header lines (get field based on column name)
  • Support for multiple line values (using the text delimiter)
  • Support for field separator character in value (using the text delimiter)
  • Support for reading and writing in an iterative or all at once way
  • Support for skipping empty rows and preserving the original line number (useful for error messages)

Requirements

  • Java 7

CsvReader Examples

Iterative reading of a CSV file (RFC standard format, UTF-8 encoded)

File file = new File("foo.csv");
CsvReader csvReader = new CsvReader();

try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
    CsvRow row;
    while ((row = csvParser.nextRow()) != null) {
        System.out.println("Read line: " + row);
        System.out.println("First column of line: " + row.getField(0));
    }
}

Read full CSV file at once (RFC standard format, UTF-8 encoded)

File file = new File("foo.csv");
CsvReader csvReader = new CsvReader();

CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
for (CsvRow row : csv.getRows()) {
    System.out.println("Read line: " + row);
    System.out.println("First column of line: " + row.getField(0));
}

Read full CSV file with header at once (RFC standard format, UTF-8 encoded)

File file = new File("foo.csv");
CsvReader csvReader = new CsvReader();
csvReader.setContainsHeader(true);

CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
for (CsvRow row : csv.getRows()) {
    System.out.println("First column of line: " + row.getField("name"));
}

Custom settings

CsvReader csvReader = new CsvReader();
csvReader.setFieldSeparator(';');
csvReader.setTextDelimiter('\'');

CsvWriter Examples

Iterative writing of a CSV file (RFC standard format, UTF-8 encoded)

File file = new File("foo.csv");
CsvWriter csvWriter = new CsvWriter();

try (CsvAppender csvAppender = csvWriter.append(file, StandardCharsets.UTF_8)) {
    // header
    csvAppender.appendLine("header1", "header2");

    // 1st line in one operation
    csvAppender.appendLine("value1", "value2");

    // 2nd line in split operations
    csvAppender.appendField("value3");
    csvAppender.appendField("value4");
    csvAppender.endLine();
}

Write full CSV file at once (RFC standard format, UTF-8 encoded)

File file = new File("foo.csv");
CsvWriter csvWriter = new CsvWriter();

Collection<String[]> data = new ArrayList<>();
data.add(new String[] { "header1", "header2" });
data.add(new String[] { "value1", "value2" });

csvWriter.write(file, StandardCharsets.UTF_8, data);

Custom settings

CsvWriter csvWriter = new CsvWriter();
csvWriter.setFieldSeparator(';');
csvWriter.setTextDelimiter('\'');
csvWriter.setLineDelimiter("\r\n".toCharArray());
csvWriter.setAlwaysDelimitText(true);

Contribution

  • Fork
  • Code
  • Add test(s)
  • Commit
  • Send me a pull request

Copyright

Copyright 2018 Oliver Siegmar

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

fastcsv's People

Contributors

charphi avatar nathankleyn avatar osiegmar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.