简体   繁体   中英

Writing List to CSV using BeanIO

I am trying to write list of data into CSV using BeanIO framework. I am able to write a single object into CSV but not able to write list of data.

I have tried the following code:

package com.beanio.example;

import java.util.Date;

public class Employee {
    String firstName;
    String lastName;
    String title;
    int salary;
    Date hireDate;

    // setters and getters follow ...
}
<beanio xmlns="http://www.beanio.org/2012/03" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

  <stream name="employeeFile" format="csv">
  <record name="header" class="com.beanio.example.GroupH" order="1" occurs="1">
      <field name="h1" default="Header1" ignore="true" />
      <field name="h2" default="Header2" ignore="true" />
      <field name="h3" default="Header2" ignore="true" />
    </record>
    <record name="employee" class="com.beanio.example.Employee"  order="2">
      <field name="firstName" />
      <field name="lastName" />
      <field name="title" />
      <field name="salary" />
      <field name="hireDate" format="MMddyyyy" />
    </record>
  </stream>
</beanio>
public class Main {
    public static void main(String[] args) throws Exception {
        // create a StreamFactory
        StreamFactory factory = StreamFactory.newInstance();
        // load the mapping file
        factory.loadResource("mapping.xml");

        List<Employee> employees = new ArrayList<Employee>();

        Employee employee = new Employee();
        employee.setFirstName("Jennifer");
        employee.setLastName("Jones");
        employee.setTitle("Marketing");
        employee.setSalary(60000);
        employee.setHireDate(new Date());

        Employee employee1 = new Employee();
        employee1.setFirstName("JACk");
        employee1.setLastName("Jones");
        employee1.setTitle("Tele");
        employee1.setSalary(60000);
        employee1.setHireDate(new Date());

        employees.add(employee);
        employees.add(employee1);

        // use a StreamFactory to create a BeanWriter
        BeanWriter out = factory.createWriter("employeeFile", new File("employee.csv"));
        // write an Employee object directly to the BeanWriter
        out.write(employees);
        out.flush();
        out.close();
    }
}

I am expecting results in the CSV to follow the format below:

FirstName,LastName,Title,Salary,HireDate

For example:

Jennifer,Jones,Marketing,60000,24-12-2016

JACK,Jones,TELE,60000,24-12-2016

Can someone please help me?

You only need to do this:

for(Employee e: employees){
    out.write(e);
}

This is the full code:

public class Main {
    public static void main(String[] args) throws Exception {
        // create a StreamFactory
        StreamFactory factory = StreamFactory.newInstance();
        // load the mapping file
        factory.loadResource("mapping.xml");

        List<Employee> employees = new ArrayList<Employee>();

        Employee employee = new Employee();
        employee.setFirstName("Jennifer");
        employee.setLastName("Jones");
        employee.setTitle("Marketing");
        employee.setSalary(60000);
        employee.setHireDate(new Date());

        Employee employee1 = new Employee();
        employee1.setFirstName("JACk");
        employee1.setLastName("Jones");
        employee1.setTitle("Tele");
        employee1.setSalary(60000);
        employee1.setHireDate(new Date());

        employees.add(employee);
        employees.add(employee1);

        // use a StreamFactory to create a BeanWriter
        BeanWriter out = factory.createWriter("employeeFile", new File("employee.csv"));
        // write an Employee object directly to the BeanWriter
        for(Employee e: employees){
            out.write(e);
        }
        out.flush();
        out.close();
    }
}

Using BeanIO you can write into flat file as single object only.

Whereas you can achieve it using Spring batch integration with BeanIO.

http://beanio.org/2.1/docs/reference/index.html#SpringItemReaderWriter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM