超级CSV Java所有数据都没有写入csv

时间:2015-09-05 08:25:07

标签: java supercsv

我有一个大小为944的列表<Bean>,当数据写入CSV文件时,一切正常,但我只看到940条记录。我不知道是什么问题。有人可以帮帮我吗?

static void writeCSVFile(String fileName, List<GPR> listGPR) {
    ICsvBeanWriter beanWriter = null;
    CellProcessor[] processors = new CellProcessor[] {
        new NotNull(), 
        new Optional(),
        new Optional(),
        new Optional(), 
        new Optional(), 
        new Optional(),
        new Optional(),
    };

    try {
        beanWriter = new CsvBeanWriter(new FileWriter(fileName),
            CsvPreference.STANDARD_PREFERENCE);

        final String[] header = {"Number","Project Type","Actual Start Date","Actual End Date","Owner","Name","Description"};

        final String[] dataMapping = new String[] { "number", "projectType", "actualStrtDate", "actualEndDate", "owner", "name","description"};

        beanWriter.writeHeader(header);
        for (GPR gpr : listGPR) {
            beanWriter.write(gpr, dataMapping, processors);
        }
    } catch (IOException ex) {
        System.out.println("Error writing the CSV file: " + ex);
    } finally {
        if (beanWriter != null) {
            try {
                beanWriter.close();
            } catch (IOException ex) {
                System.out.println("Error closing the writer: " + ex);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,将代码放入try catch块中并首先关闭writeToCSV(writeToCSV.close())对象引用有助于我获取所有记录。