如何使用bean io将固定长度文件写入CSV文件,所有值都来自记录的不同列

how to write fixedlength file to CSV file with bean io with all values come in diffent columns of a record

此代码能够将数据写入 csv 文件,但唯一的问题是数据仅写入单列。

我希望数据出现在不同的列中。我是 bean io 的新手,无法弄清楚。

我尝试了下面给定的代码,但无法以正确的格式获得输出:

public class XlsWriter {

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

          Field[] fields = Employee.class.getDeclaredFields();

          System.out.println("fileds" + fields.length);
          List<Object> list = new ArrayList<Object>();
          for (Field field : fields) {

                 list.add(field.getName());

          }


          BeanReader in = factory.createReader("EmployeeInfo", new File("C:\Temp\Soc\textInput.txt"));




          BeanWriter out = factory.createWriter("EmployeeInfo", new File("C:\Temp\Soc\output.csv"));

          Object record;

          while ((record = in.read()) != null) {
                 System.out.println(record.toString().length());


                 out.write(record);

                 System.out.println("Record Written:" + record.toString());

          }

          in.close();
          out.flush();
          out.close();
   }


}

textInput.txt
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC



<?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="employee"
                 class="com.aexp.gmnt.imc.record.submission.Employee" minOccurs="0"
                 maxOccurs="unbounded" order="1">
                 <field name="firstName" length="5" padding="0" justify="right" />
                 <field name="lastName"  length="5" padding="0" justify="right"/>
                 <field name="title" length="5" padding="0" justify="right"/>

          </record>
   </stream>

我想要 CSV 文件的不同列中的每个记录值,但目前它只出现在单个列中,请帮忙。

您的映射文件中需要有不同的流定义才能写入 CSV 文件。 EmployeeInfo 流只能处理固定长度的内容,因为这是它的配置方式。

您需要添加第二个 <stream> 定义来处理您要生成的 CSV 文件,并且您的 BeanWriter 需要引用新的 CSV 流而不是固定长度的 CSV 流。

向现有 mapping.xml 文件添加新的 <stream> 定义:

<stream name="EmployeeInfoCSV" format="csv">
  <record name="employee" class="com.aexp.gmnt.imc.record.submission.Employee" minOccurs="0" maxOccurs="unbounded">
    <field name="firstName" />
    <field name="lastName" />
    <field name="title" />
  </record>
</stream>

请注意 <stream> 的名称更改,format 设置为 csv。在此 <stream> 定义中,如果您愿意,您还可以更改数据写入 csv 文件的顺序,而不会影响 BeanReader 预期读取数据的顺序。 csv 文件不需要 lengthpaddingjustify 属性。

现在您只需要更改配置您的 BeanWriter 的方式:

BeanWriter out = factory.createWriter("EmployeeInfo", new File("C:\Temp\Soc\output.csv"));

BeanWriter out = factory.createWriter("EmployeeInfoCSV", new File("C:\Temp\Soc\output.csv"));

请注意在 createWriter 方法参数中使用 csv 流名称的更改。


编辑 从评论中回答这个问题:

just a added question of I need to add the as first line with header values as field values without writing them as header record type in bean io then is it possible through reflection or something?

无需思考或跳过障碍即可完成。您可以创建一个 Writer,您可以使用它首先将 header(列)名称写入文件,然后再将编写器传递给 BeanWriter 以附加其余输出。

而不是像上面那样使用 BeanWriter

BeanWriter out = factory.createWriter("EmployeeInfoCSV", new File("C:\Temp\Soc\output.csv"));

你现在会做这样的事情:

BufferedWriter writer = new BufferedWriter(new FileWriter(new File("C:\Temp\Soc\output.csv")));
writer.write("First Name,Last Name,Title");
writer.newLine();

BeanWriter out = factory.createWriter("EmployeeInfoCSV", writer);

BeanIO 然后继续将其输出写入 writer,后者会将数据附加到现有文件。完成后记得close()作者。