Java 要从beanIO字段名称标记以csv格式写入标题吗

Java 要从beanIO字段名称标记以csv格式写入标题吗,java,bean-io,Java,Bean Io,我想在csv文件中写一个头,因为我的文本文件不包含任何头,所以我想从beanIO字段名标记中写它 我有一个双流的beanIO,一个用于阅读,另一个用于写作 这是输入文件。。。。 textInput.txt- 约翰·杜巴xxx 上午1点,下午2点 public static void main(String[] args) throws Exception { StreamFactory factory = StreamFactory.newInstance(); factor

我想在csv文件中写一个头,因为我的文本文件不包含任何头,所以我想从beanIO字段名标记中写它

我有一个双流的beanIO,一个用于阅读,另一个用于写作

这是输入文件。。。。 textInput.txt-
约翰·杜巴xxx
上午1点,下午2点

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>
publicstaticvoidmain(字符串[]args)引发异常{
StreamFactory=StreamFactory.newInstance();
load(“C:\\Users\\PV5057094\\Demo\u workspace\\XlsxMapper\\src\\main\\resources\\Employee.xml”);
BeanReader br=factory.createReader(“EmployeeInfo”,新文件(“C:\\Temp\\Soc\\textInput.txt”);
BeanWriter out=factory.createWriter(“EmployeeInfoCSV”,新文件(“C:\\Temp\\Soc\\output.csv”);
实物记录;
而((record=br.read())!=null){
写出(记录);
System.out.println(“记录写入:+Record.toString());
}
//in.close();
out.flush();
out.close();
}
}
比尼奥-
预期产量-

记录、名字、姓氏、标题、填充符
1、john、dew、BA、xxx

1、sam、hart、MA、yyy

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

<record name="headers" minOccurs="1" maxOccurs="1">
  <field name="recordColumn" default="Record"/>
您还必须将CSV流中
a
记录上的
length
属性更改为
maxLength
,否则您将在输出上获得填充,它看起来仍然是固定长度格式

改变

<field name="firstName" length="5"/>
和映射文件:

<?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>

我编写了一个utils方法,可以根据原始记录中设置的
@Field.name
属性动态创建头记录,这可能被认为是一种黑客行为,但这比创建一个新类或创建一个XML文件来打印CSV头行要好

import java.io.ByteArrayOutputStream;
导入java.io.OutputStreamWriter;
导入org.beanio.BeanWriter;
导入org.beanio.StreamFactory;
导入org.beanio.annotation.Field;
导入org.beanio.annotation.Record;
导入org.beanio.builder.FieldBuilder;
导入org.beanio.builder.RecordBuilder;
导入org.beanio.builder.StreamBuilder;
公共类标题{
公共静态void main(字符串[]args){
最终字符串factoryName=“逗号分隔的csv工厂”;
最终字符串headerName=“CarHeader”;
最终var生成器=新StreamBuilder(factoryName)
.格式(“csv”)
.addRecord(标题(汽车类别、标题名称))
.addRecord(汽车类)
;
final var factory=StreamFactory.newInstance();
工厂定义(生成器);
final ByteArrayOutputStream bout=新ByteArrayOutputStream();
最终BeanWriter编写器=factory.createWriter(factoryName,新的OutputStreamWriter(bout));
试一试{
writer.write(headerName,null);
作者:write(新车(“福特卡”,2016年));
作者:write(新车(“福特Fusion”,2020年));
}最后{
writer.close();
}
System.out.println(bout.toString());
//型号,年份
//福特卡,2016年
//福特Fusion,2020年
}
的公共静态记录生成器(类clazz,字符串名称){
最终记录生成器=新记录生成器(名称)
.命令(1);
if(clazz.getAnnotation(Record.class)==null){
抛出新的IllegalArgumentException(“类必须是BeanIo记录,用@Record注释”);
}
for(java.lang.reflect.Field类字段:clazz.getDeclaredFields()){
最终字段fieldAnnotation=classField.getAnnotation(Field.class);
if(fieldAnnotation==null){
继续;
}
builder.addField(
新的FieldBuilder(fieldAnnotation.name())
.defaultValue(fieldAnnotation.name())
);
}
返回生成器;
}
@记录(顺序=2)
静态级轿车{
@字段(name=“Model”)
私有字符串模型;
@字段(name=“Year”)
私人整数年;
公共汽车(字符串模型,整数年){
this.model=模型;
今年=年;
}
公共字符串getModel(){
收益模型;
}
公共整数getYear(){
回归年;
}
公共车辆集合模型(字符串模型){
this.model=模型;
归还这个;
}
公共车辆设置年(整数年){
今年=年;
归还这个;
}
}
}

请添加您当前的输出,还有一件事,如果我在BeanIO中有段或列表,该段我必须打印100次,这是否有可能?这应该是一个新问题:-)BeanIO可以处理它。这个片段是你现有记录中的一个片段吗?我有一个海格,我真的认为你应该问一个新问题,解释你尝试了什么,输入和预期输出是什么,就像你在这个问题上做的那样。解释该部分涉及的内容等。
<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>