CSV and set object are encapsulated based on Annotation

Source: Internet
Author: User

With the release of the project, the project is currently idle, so you can take the team to summarize and extract the components, tools, and practices that have been done well in this year's project, I will release it later. Today, it is mainly a simple maven small component that encapsulates opencsv Based on Annotation, this allows us to easily convert CSV files into List objects and Export List objects as CSV files.

The project hosting address is at github https://github.com/green?g/opencsv-utils.

You don't need to talk about the code. Let's take a look at how to use it.

Object


Package opencsv. utils;


Public class Person {


Private int id;


@ Csv ("person name ")

Private String name;


@ Ignore

Private int age;


Public Person (int id, String name, int age ){

This. id = id;

This. name = name;

This. age = age;

}


Public Person (){


}


Public int getId (){

Return id;

}


Public void setId (int id ){

This. id = id;

}


Public String getName (){

Return name;

}


Public void setName (String name ){

This. name = name;

}


Public int getAge (){

Return age;

}


Public void setAge (int age ){

This. age = age;

}

}


Mapping automatically uses fields without ignore as the CSV ing attribute name as the CSV column header. If special columns are used, @ CSV can be marked.

1: Read CSV:

(1) Annotation-based aning

@ Test

Public void shouldGetPersonFromCSV () throws Exception {

StringReader reader = new StringReader ("id, person name \ n1, name1 \ n2, name2 \ n ");

List <Person> personList = personCsvMapper

. WithMapping ("id", "id ")

. WithMapping ("person name", "name ")

. FromCsv (reader );


AssertThat (personList. size (), is (2 ));


Final Person first = personList. get (0 );

AssertThat (first. getId (), is (1 ));

AssertThat (first. getName (), is ("name1 "));


Final Person second = personList. get (1 );

AssertThat (second. getId (), is (2 ));

AssertThat (second. getName (), is ("name2 "));


}



(2) custom ing

@ Test

Public void shouldToCsv () throws Exception {

PersonCsvMapper. withMapping (new CsvColumnMapping (Person. class ));

Final ArrayList <Person> list = new ArrayList <Person> ();

List. add (new Person (1, "name1", 20 ));

List. add (new Person (2, "name2", 30 ));

Final StringWriter writer = new StringWriter ();


PersonCsvMapper. toCsv (writer, list );


Final String text = writer. toString ();

AssertThat (text, is ("id, person name \ n1, name1 \ n2, name2 \ n "));

}

2: CSV output

@ Test

Public void shouldGetPersonFromCsv () throws Exception {

StringReader reader = new StringReader ("id, person name \ n1, name1 \ n2, name2 \ n ");

List <Person> personList = new CsvMapper <Person> (Person. class)

. WithMapping (new CsvColumnMapping (Person. class ))

. FromCsv (reader );


AssertThat (personList. size (), is (2 ));


Final Person first = personList. get (0 );

AssertThat (first. getId (), is (1 ));

AssertThat (first. getName (), is ("name1 "));


Final Person second = personList. get (1 );

AssertThat (second. getId (), is (2 ));

AssertThat (second. getName (), is ("name2 "));


}


Finally, the hosted address is https://github.com/green?g/opencsv-utils. You don't need to talk about anything else.


This article from the "wolf" blog, please be sure to keep this source http://whitewolfblog.blog.51cto.com/3973901/1202150

Related Article

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.