0

我想读取所有具有固定列号但 2 个不同文件具有 2 个不同列号的不同 CSV 文件。所有文件都有一个标题行。所以我首先使用 CSVListReader 来获取标题和列号,然后构造单元处理器和 CSV BeanReader 以将实际行映射到 POJO。我首先尝试通过将 InputStreamReaders 传递给 superCsv 读取器构造函数来使其工作,但它不起作用。但与 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;
}

提前致谢

4

1 回答 1

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);
        }
    }
于 2013-09-18T10:25:52.097 回答