3 Replies Latest reply on Feb 24, 2017 11:49 AM by Cheng Fang

    how to fit  in jackson csvitem writer

    sunitha N Newbie

      I got class  which has got pojo.Is there way to feed in jacksoncsvItem writer.

       

      import com.fasterxml.jackson.annotation.JsonFormat;

       

      import com.fasterxml.jackson.annotation.JsonPropertyOrder;

      import com.fasterxml.jackson.databind.ObjectWriter;

      import com.fasterxml.jackson.dataformat.csv.CsvMapper;

      import com.fasterxml.jackson.dataformat.csv.CsvSchema;

      import java.io.BufferedOutputStream;

      import java.io.File;

      import java.io.FileNotFoundException;

      import java.io.FileOutputStream;

      import java.io.IOException;

      import java.io.OutputStreamWriter;

      import java.io.UnsupportedEncodingException;

      import java.util.ArrayList;

      import java.util.Date;

      import java.util.List;

       

      public class CsvCreator {

       

      public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {

       

      // POJO (bean class)

      @JsonPropertyOrder({ "name", "age", "dob" })

      class User {

      public String name;

      public int age;

      public Date dob;

       

      public User() {

      }

       

      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")

      public Date getDob() {

      return dob;

      }

       

      public void setDob(Date dob) {

      this.dob = dob;

      }

       

      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;

      }

      }

       

      long DAY_IN_MS = 1000 * 60 * 60 * 24;

       

      // define objects

      User user1 = new User();

      user1.name = "foo";

      user1.age = 32;

      user1.dob = new Date(System.currentTimeMillis() - (30 * DAY_IN_MS));

       

      User user2 = new User();

      user2.name = "bar";

      user2.age = 27;

      user2.dob = new Date(System.currentTimeMillis() - (7 * DAY_IN_MS));

       

      List<User> users = new ArrayList<User>();

      users.add(user1);

      users.add(user2);

       

      // create mapper and schema

      CsvMapper mapper = new CsvMapper();

      CsvSchema schema = mapper.schemaFor(User.class).withColumnSeparator('|').withoutQuoteChar();

       

      // output writer

      ObjectWriter myObjectWriter = mapper.writer(schema);

      File tempFile = new File("users.csv");

      FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);

      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(tempFileOutputStream, 1024);

      OutputStreamWriter writerOutputStream = new OutputStreamWriter(bufferedOutputStream, "UTF-8");

      myObjectWriter.writeValue(writerOutputStream, users);

      }

      }