在superCSV中使用CSVListReader和CSVBeanreader

时间:2013-09-10 07:28:53

标签: csv supercsv

我想阅读不同的CSV文件,这些文件具有固定的列号,但是2个不同的文件具有2个不同的列号。所有文件都有标题。 所以我首先使用CSVListReader获取标题和列号,然后构造单元格处理器和CSV BeanReader以将实际行映射到POJO。 我首先尝试将InputStreamReaders传递给superCsv reader构造函数,但它不起作用。但是使用FileReaders或BufferedReaders工作正常。 它是一个错误还是在这种情况下使用InputStremReaders没有意义?

这是工作代码示例

CsvListReader listReader = null;
FileReader file = null;
BufferedReader b = null;
try {
    file = new FileReader(linkToFile);
    b = new BufferedReader(file);
    listReader = new CsvListReader(b,
            CsvPreference.STANDARD_PREFERENCE);
    csvHeader = listReader.getHeader(true);
} catch (IOException e) {
    logger.info("Did not manage to get the Csv Header", e);
    try {
        listReader.close();
        file.close();
    } catch (IOException e1) {
        logger.info("Problem trying to close the readers", e1);
        return;
    }

  }
try {
    file = new FileReader(linkToFile);
    b = new BufferedReader(file);
    beanReader = new CsvBeanReader(b,
            CsvPreference.STANDARD_PREFERENCE);
    beanReader.getHeader(false);

    extractCSV(beanReader, csvHeader);
 catch (IOException e) {
    logger.info("Did not manage to get a working CsvBeanReader.", e);
    try {
        beanReader.close();
        listReader.close();
        file.close();
    } catch (IOException e1) {
        logger.info("Problem trying to close the readers", e1);
    }
    return;
}

提前致谢

1 个答案:

答案 0 :(得分:0)

根据Hound Doc Comments,陷入困境的原因在于关闭不同读者的糟糕管理。 以下是使用输入流阅读器的工作代码

   // Reading the Header. A CsvListReader object is used here as it can
    // read a variable number of columns in the first line (see
    // http://supercsv.sourceforge.net/readers.html)
    CsvListReader listReader = null;
    InputStreamReader b = null;
    try {
        b = new InputStreamReader(new BufferedInputStream(new FileInputStream(linkToFile)));
        listReader = new CsvListReader(b, CsvPreference.STANDARD_PREFERENCE);
        csvHeader = listReader.getHeader(true);
    } catch (IOException e) {
        logger.info("Did not manage to get the Csv Header", e);
    } finally {
        try {
            listReader.close();
        } catch (IOException e1) {
            logger.info("Problem trying to close the readers", e1);
            return;
        }
    }

    // Using the CSV bean reader to read the file. Now we know the number of
    // columns
    // A CsvBeanReader object is the choice to extract easier to POJO
    // structure
    CsvBeanReader beanReader = null;

    try {
        b = new InputStreamReader(new BufferedInputStream(new FileInputStream(linkToFile)));
        beanReader = new CsvBeanReader(b, CsvPreference.STANDARD_PREFERENCE);
        // beanReader starts reading from line 2 (see above)
        // it is as if we would be reading a file without a header
        beanReader.getHeader(false);

        extractCSVContacts(beanReader, csvHeader);

    } catch (IOException e) {
        logger.info("Did not manage to get a working CsvBeanReader.", e);
       return;
    }
    finally {
        try {
            beanReader.close();
        } catch (IOException e1) {
            logger.info("Problem trying to close the readers", e1);
        }
    }