使用 BeanIO 将列表写入 CSV



我正在尝试使用BeanIO框架将数据列表写入CSV中。我能够将单个对象写入CSV,但无法写入数据列表。

我尝试了以下代码:

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();
    }
}

我希望 CSV 中的结果遵循以下格式:

FirstName,LastName,Title,Salary,HireDate

例如:

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

有人可以帮我吗?

你只需要这样做:

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

这是完整的代码:

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();
    }
}

使用 BeanIO,您只能将平面文件作为单个对象写入。

而您可以使用与BeanIO的Spring批处理集成来实现它。

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

相关内容

  • 没有找到相关文章

最新更新