How To Read Too Write Csv Using Jackson Library

The sample code below demonstrates how nosotros tin read a csv file into an array of objects. Also it writes an array of objects into csv.

To role don't forget to include inward your pom.xml
 <dependency>  <groupId>com.fasterxml.jackson.dataformat</groupId>  <artifactId>jackson-dataformat-csv</artifactId>  <version>2.7.0</version> </dependency> 

import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.ArrayList; import java.util.List;  import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.MappingIterator; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvSchema;  /**  * @author Edward P. Legaspi  **/ world bird CsvTest {   person concluding String FILE_NAME = "offerTemplateCategory.csv";   world static void main(String args[]) {   essay {    CsvTest app = novel CsvTest();    app.testCsvRead();    app.testCsvWrite();   } choose grip of (Exception e) {    e.printStackTrace();   }  }   person void testCsvRead() throws Exception {   System.out.println("read csv");    // charge file from resources   ClassLoader classLoader = getClass().getClassLoader();   File file = novel File(classLoader.getResource(FILE_NAME).getFile());    // configure the schema nosotros desire to read   CsvSchema schema = CsvSchema.builder().addColumn("parentCategoryCode").addColumn("code").addColumn("name").addColumn("description").build();   CsvMapper mapper = novel CsvMapper();    // configure the reader on what edible bean to read as well as how nosotros desire to write   // that edible bean   ObjectReader oReader = mapper.readerFor(OfferTemplateCategory.class).with(schema);    // read from file   essay (Reader reader = novel FileReader(file)) {    MappingIterator mi = oReader.readValues(reader);    patch (mi.hasNext()) {     System.out.println(mi.next());    }   }  }   person void testCsvWrite() throws Exception {   // initialize our listing   List listing = novel ArrayList<>();   list.add(populateOfferCat(1));   list.add(populateOfferCat(2));   list.add(populateOfferCat(3));    // initialize as well as configure the mapper   CsvMapper mapper = novel CsvMapper();   // nosotros ignore unknown fields or fields non specified inward schema, otherwise   // writing volition neglect   mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);    // initialize the schema   CsvSchema schema = CsvSchema.builder().addColumn("parentCategoryCode").addColumn("code").addColumn("name").addColumn("description").build();    // map the edible bean amongst our schema for the author   ObjectWriter author = mapper.writerFor(OfferTemplateCategory.class).with(schema);    File tempFile = novel File("c://temp//output.csv");   // nosotros write the listing of objects   writer.writeValues(tempFile).writeAll(list);  }   /**   * Initialize an OfferTemplateCategory using index every bit suffix.   *    * @param index   * @return   */  person OfferTemplateCategory populateOfferCat(int index) {   OfferTemplateCategory o1 = novel OfferTemplateCategory();   o1.setParentCategoryCode("PARENT_" + index);   o1.setCode("CAT_" + index);   o1.setName("CAT_NAME_" + index);   o1.setDescription("CAT_DESCRIPTION_" + index);    provide o1;  }  }  
Just practise your OfferTemplateCategory bird amongst the fields inward schema (parentCategoryCode, code, name, description). Maybe add together closed to other plain unknown. Try to take mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true) as well as you'll run into the mistake I've mentioned.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.