I have a list of objects let's say List of Product which I get from my Db. Im able to write those List to a CSV file using openCSV StatefulBeanToCSV writer for 0.5M records it takes less than 10s to write the data to CSV file.. but the problem is StatefulBeanToCSV writes all those 0.5M records to a single CSV file whose size becomes larger.. so I need a way to restrict the CSV file to 20Mb. If the size of the CSV file reaches 20mb I need StatefulBeanToCSV to write to a new CSV file. any help is highly appreciated.
public static void buildProductCsv(final List<Product> product,
final String filePath) {
try {
Writer writer = new FileWriter(filePath);
// mapping of columns with their positions
ColumnPositionMappingStrategy<Product> mappingStrategy = new ColumnPositionMappingStrategy<Product>();
// Set mappingStrategy type to Product Type
mappingStrategy.setType(Product.class);
// Fields in Product Bean
String[] columns = new String[] { "productCode", "MFD", "EXD" };
// Setting the colums for mappingStrategy
mappingStrategy.setColumnMapping(columns);
StatefulBeanToCsvBuilder<Product> builder = new StatefulBeanToCsvBuilder<Product>(writer);
StatefulBeanToCsv<Product> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
// Writing data to csv file
beanWriter.write(product);
writer.close();
log.info("Your csv file has been generated!");
} catch (Exception ex) {
log.warning("Exception: " + ex.getMessage());
}
}