Giter Club home page Giter Club logo

quickcsv's Introduction

QuickCsv

Quick and easy-of-use handling of comma-separated values (CSV) as described in RFC 4180.
Common Format and MIME Type for Comma-Separated Values (CSV) Files

Build Status CircleCI Codeship Status for apercova/QuickCsv

Setting it Up

Setup on local /.m2 repository

Note: Currently, QuickCsv is not available on Maven Central.

To setup on your local repository, you need to follow this steps:

  • Clone or download the repository.
  • Unzip the content in case of download.
  • Change your current directory to the QuickCsv dir.
  • Execute:
        $ mvn clean install

Setup on Maven project

Add the dependency in your pom.xml

 	<dependency>
  		<groupId>net.apercova</groupId>
  		<artifactId>quickcsv</artifactId>
  		<version>1.0.1712</version>
  	</dependency>

See API: (https://apercova.github.io/QuickCsv/apidocs/)

See docs on the wiki: (https://github.com/apercova/QuickCsv/wiki)

See complete usecases code: (https://github.com/apercova/QuickCsv/tree/master/usecases)


Reading a file the quickest way:

Up to java 1.6

    Reader reader = null;
    try {
        //Getting a reader for CsvFile.csv
        reader = new InputStreamReader(
                new FileInputStream("CsvFile.csv"), 
                Charset.forName("utf-8"));
        
        //Values are read as a row list
        List<List<String>> values = CsvReader.read(reader);
        
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Can't perform reading", e);
        
    } catch (CsvReaderException e) {
        logger.log(Level.SEVERE, "Can't perform reading", e);
        
    } finally {
        try {
            if(reader != null)
                reader.close();
            reader = null;
        } catch (IOException e) {
            logger.log(Level.FINE, "Unable to close reader", e);	
        }
    }

Using java 1.7+

    try (
        //Getting a reader for CsvFile.csv
        Reader reader = new InputStreamReader(
                new FileInputStream("CsvFile.csv"),
                Charset.forName("utf-8"));				
        ) {
    
        //Values are read as a row list
        List<List<String>> values = CsvReader.read(reader);
    
    } catch (IOException | CsvReaderException e) {
        logger.log(Level.SEVERE, "Can't perform reading", e);
    }

See more csv reading usecases here.


Writing a file the quickest way:

Up to java 1.6

    Writer writer = null;
    try {
        //Getting a writer for Countries.csv
        writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("Countries.csv"), 
                Charset.forName("utf-8")));
        
        List<List<String>> values = new LinkedList<List<String>>();
        values.add(Arrays.asList(new String[] {"ISO_CODE","NAME","CAPITAL"}));
        values.add(Arrays.asList(new String[] {"US","United States of America",""}));
        values.add(Arrays.asList(new String[] {"MX","Estados Unidos Mexicanos","Ciudad de México, \"CDMX\""}));
        values.add(Arrays.asList(new String[] {"AU","Austalia","Sidney"}));
        
        //Writing out values
        CsvWriter.write(writer, values, true);//autoflush
        
        //also direct flush
        writer.flush();
        
    } catch(IOException e) {
        logger.log(Level.SEVERE, "Can't perform writing", e);
    } catch (CsvWriterException e) {
        logger.log(Level.SEVERE, "Can't perform writing", e);
    } finally {
        try {
            if(writer != null)
        		    writer.close();
                writer = null;
        } catch (IOException e) {
            logger.log(Level.FINE, "Failed to close resource", e);	
        }
    }

Using java 1.7+

    try (
        //Getting a writer for Countries.csv
        Writer writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("Countries.csv"), 
                    Charset.forName("utf-8")));
        ){
        
        List<List<String>> values = new LinkedList<List<String>>();
        values.add(Arrays.asList(new String[] {"ISO_CODE","NAME","CAPITAL"}));
        values.add(Arrays.asList(new String[] {"US","United States of America",""}));
        values.add(Arrays.asList(new String[] {"MX","Estados Unidos Mexicanos","Ciudad de México, \"CDMX\""}));
        values.add(Arrays.asList(new String[] {"AU","Austalia","Sidney"}));
        
        //Writing out values
        CsvWriter.write(writer, values, true);//autoflush
        
        //also direct flush
        writer.flush();
        
    } catch(IOException | CsvWriterException e) {
        logger.log(Level.SEVERE, "Can't perform writing", e);
    } 

See more csv writing usecases here.

quickcsv's People

Contributors

apercova avatar

Watchers

 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.