我正在尝试使用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
有人可以帮帮我吗?
答案 0 :(得分:1)
你只需要这样做:
my_data = rnorm(155)
my_data_ts <- ts(my_data, start = c(2002, 10), frequency = 12)
这是完整的代码:
for(Employee e: employees){
out.write(e);
}
答案 1 :(得分:0)
使用BeanIO,您只能将平面文件写入单个对象。
您可以使用与BeanIO的Spring批处理集成来实现它。
http://beanio.org/2.1/docs/reference/index.html#SpringItemReaderWriter