user3632475
user3632475

Reputation: 21

Writing bean values to CSV using BeanIO Framework

I am writing bean file values to a CSV file using BeanIO.jar. I am not getting any error but the contents are not written to CSV File.

Please find my code and help me

  public class CSVStreamWriter {

   public void splitandWrite(List<Orders> orderList) {
        StreamFactory factory = StreamFactory.newInstance();
        factory.load("mapping.xml");
        BeanWriter writer = factory.createWriter("orders", new File(
                "C:\\sales.csv"));
        Orders orders = new Orders();
        orders.setCustomerId("1");
        orders.setCustomerName("krish");
        orders.setPackages("5");
        orders.setPackageType("units");
        orders.setProductName("oreo");
        orders.setQuantity("100");

        writer.write(orders);

    }
}

My mapping file

<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03">
    <stream name="orders" format="csv">
        <record name="orders" class="com.tedge.mc.Orders">
            <field name="customerId" />
            <field name="customerName" />
            <field name="quantity" />
            <field name="productName" />
            <field name="packages" />
            <field name="packageType" />
        </record>
    </stream>
</beanio>

Upvotes: 2

Views: 2387

Answers (1)

user180100
user180100

Reputation:

Because you don't close nor flush the BeanWriter, the content you wrote is not flushed into the file.

You should close your writer (that will do the flush):

writer.write(orders);
writer.close();

Upvotes: 2

Related Questions