Apache Commons csv Write file demo

Source: Internet
Author: User

1. Introduction

There are many tools for working with open source CSV, such as Super CSV and Apache Commons CSV, this article uses Apache Commons CSV, which provides a very simple interface to read and write CSV files.
This article is a write example for a CSV file.

2. Reliance

<dependency>      <groupId>org.apache.commons</groupId>      <artifactId>commons-csv</artifactId>      <version>1.1</version>    </dependency>

3.pojo objects

Map the data that needs to be saved to the object, and then write the object information to the CSV file.

 PackageTest.csv;ImportJava.math.BigDecimal;/** * City Rate Model * * Created by Qunzer on 2015/3/3. * * Public  class cityprice {    PrivateInteger ID;PrivateString City;PrivateBigDecimal Price; Public Cityprice(Integer ID, String city, BigDecimal Price) { This. id = ID; This. City = City; This. Price = Price; } PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; } PublicStringgetcity() {returnCity } Public void setcity(String city) { This. City = City; } PublicBigDecimalGetPrice() {returnPrice } Public void Setprice(BigDecimal Price) { This. Price = Price; }}

4.csv Write code example

 PackageTest.csv;Importcom.google.common.collect.Lists;ImportOrg.apache.commons.csv.CSVFormat;ImportOrg.apache.commons.csv.CSVPrinter;ImportJava.io.FileWriter;ImportJava.io.IOException;ImportJava.math.BigDecimal;ImportJava.util.List;/** * Created by Qunzer on 2015/3/3. * * Public  class csvfilewriter {    Private Static FinalString Line_separator ="\ n";Private Static FinalString[] File_heand = {"Number","City","Room Rate"};/** * Initialize the data, such as from the database value and so on * @return  * *    Private StaticList<cityprice>Precitypricedata() {list<cityprice> citypricelist = lists.newarraylist (); Cityprice Cityprice =NewCityprice (1,"Beijing", Bigdecimal.valueof (350000)); Citypricelist.add (Cityprice);returnCitypricelist; }/** * Write to CSV file * * @param filename of the path to the file, including file name */    Private Static void Csvwriter(String fileName)        {FileWriter FileWriter; Csvprinter Csvprinter =NULL; Csvformat Csvformat = CSVFormat.DEFAULT.withRecordSeparator (line_separator);Try{FileWriter =NewFileWriter (FileName); Csvprinter =NewCsvprinter (FileWriter, Csvformat); Csvprinter.printrecord (File_heand); for(Cityprice cityprice:precitypricedata ())                {list<string> Citypricerecord = lists.newarraylist ();                Citypricerecord.add (String.valueof (Cityprice.getid ()));                Citypricerecord.add (Cityprice.getcity ());                Citypricerecord.add (String.valueof (Cityprice.getprice ()));            Csvprinter.printrecord (Citypricerecord); }        }Catch(IOException e)        {E.printstacktrace (); }finally{Try{if(Csvprinter! =NULL) {Csvprinter.flush ();                Csvprinter.close (); }            }Catch(IOException e)            {E.printstacktrace (); }        }    }}

Apache Commons csv Write file demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.