gpt4 book ai didi

java - 想要从 beanIO 字段名称标签写入 csv 中的 header

转载 作者:行者123 更新时间:2023-12-02 02:36:06 27 4
gpt4 key购买 nike

我想在 csv 文件中写入标题,因为我的文本文件不包含任何标题,所以我想从 beanIO 字段名称标签写入它

我有一个 beanIO,有两个流,一个用于读取,另一个用于写入

这是输入文件....textInput.txt-
1约翰·露 BA xxx
1sam 哈特 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>

预期输出-

记录、名字、姓氏、标题、填充
1,约翰,露,BA,xxx
1、萨姆、哈特、MA、yyy

最佳答案

您必须在 EmployeeInfoCSV 流定义中定义一个新的记录,其中将包含列名称作为字段的默认值,例如

<record name="headers" minOccurs="1" maxOccurs="1">
<field name="recordColumn" default="Record"/>

然后你必须告诉你的BeanWriter在输出文件的其余部分之前先写出“头”记录。

out.write("headers", null);

您还必须将 CSV 流中的 a 记录的 length 属性更改为 maxLength,否则您将得到填充输出,它仍然看起来像固定长度格式。

改变

<field name="firstName" length="5"/>

<field name="firstName" maxLength="5"/>

将这些放在一起:

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"));

// write the column headers to the output file
out.write("headers", null);

Object record;
while ((record=br.read())!=null) {
out.write(record);
System.out.println("Record Written:" + record.toString());
}

br.close(); // yes, also close the reader
out.flush();
out.close();
}

以及映射文件:

<?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="headers" minOccurs="1" maxOccurs="1">
<field name="recordColumn" default="Record"/>
<field name="firstNameColumn" default="FirstName"/>
<field name="lastNameColumn" default="LastName"/>
<field name="titleColumn" default="Title"/>
<field name="fillerColumn" default="Filler"/>
</record>
<record name="a" minOccurs="0" maxOccurs="unbounded"
class="com.aexp.gmnt.imc.record.submission.Employee">
<field name="record" length="1"/>
<field name="firstName" maxLength="5"/>
<field name="lastName" maxLength="5"/>
<field name="title" maxLength="5"/>
<field name="filler" maxLength="5"/>
</record>
</stream>
</beanio>

关于java - 想要从 beanIO 字段名称标签写入 csv 中的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57200966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com