我想在 csv 文件中写一个标题,因为我的文本文件不包含任何标题,所以我想从 beanIO 字段名称标签写入它
我有一个带有两个流的 beanIO,一个用于读取,另一个用于写入
这是输入文件.... textInput.txt-
1john dew BA xxx
1sam hart MA yyy
public static void main(String[] args) throws Exception {
StreamFactory factory = StreamFactory.newInstance();
factory.load("C:\\Users\\PV5057094\\Demo_workspace\\XlsxMapper\\src\\main\\resources\\Employee.xml");
BeanReader br = factory.createReader("EmployeeInfo",new File("C:\\Temp\\Soc\\textInput.txt"));
BeanWriter out = factory.createWriter("EmployeeInfoCSV", new File("C:\\Temp\\Soc\\output.csv"));
Object record;
while ((record=br.read())!=null) {
out.write(record);
System.out.println("Record Written:" + record.toString());
}
// in.close();
out.flush();
out.close();
}
}
BeanIO-
<?xml version="1.0" encoding="UTF-8"?>
<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="EmployeeInfo" format="fixedlength">
<record name="a" minOccurs="0" maxOccurs="unbounded"
class="com.aexp.gmnt.imc.record.submission.Employee">
<field name="record" length="1" literal="1" rid="true"/>
<field name="firstName" length="5"/>
<field name="lastName" length="5"/>
<field name="title" length="5"/>
<field name="filler" length="5"/>
</record>
</stream>
<stream name="EmployeeInfoCSV" format="csv">
<record name="a" minOccurs="0" maxOccurs="unbounded"
class="com.aexp.gmnt.imc.record.submission.Employee">
<field name="record" length="1" literal="1" rid="true"/>
<field name="firstName" length="5"/>
<field name="lastName" length="5"/>
<field name="title" length="5"/>
<field name="filler" length="5"/>
</record>
</stream>
</beanio>
预期产出-
Record,FirstName,LastName,Title,Filler
1,john,dew,BA,xxx
1,sam,hart,MA,yyy