如何使用OpenCSV从输入CSV文件中提取标头?

发布于 2025-02-09 15:00:15 字数 1538 浏览 1 评论 0原文

opencsv 是用于阅读和写作 csv文件。我想阅读一个CSV文件并在处理数据记录之前提取CSV文件的标题。如何做?

这是我当前的代码,需要标题信息:

BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream()));

CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();

CSVReader csvReader = new CSVReaderBuilder(in).withCSVParser(csvParser).build();

// The code that follows depends on the column names, so
// I want to get the header information here so I can
// discover if I have to map to the Book entity class or to another one.

CsvToBean<Book> csvToBeanConverter = new CsvToBeanBuilder<Book>(csvReader).withType(Book.class).build();

Iterator<Book> bookIter = csvToBeanConverter.iterator();

bookIter.forEachRemaining(book -> {
    System.out.println("book: " + book);
});

更新1
我开始认为,两个“标准”解决方案OpenCSV和Apache-Commons-CSV都有其弱点,因此请随时告诉我您最喜欢的CSV库。将它们评论而不是答案,因为根据定义,它是自以为是的信息。

更新2
如何读取CSV标题并将其输入Java中的列表只是这个问题的解决方案,因为使其与csvtobean一起使用,它涉及mark()和reset()bufferedReader上,因此它限制了可能的标头大小,如果超过了,则抛出异常。有关详细信息,请参见我的答案。

OpenCSV is a java library for reading and writing CSV files. I want to read a CSV file and extract the header of the CSV file before handling the data records. How to do this?

This is my current code and where I need the header information:

BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream()));

CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();

CSVReader csvReader = new CSVReaderBuilder(in).withCSVParser(csvParser).build();

// The code that follows depends on the column names, so
// I want to get the header information here so I can
// discover if I have to map to the Book entity class or to another one.

CsvToBean<Book> csvToBeanConverter = new CsvToBeanBuilder<Book>(csvReader).withType(Book.class).build();

Iterator<Book> bookIter = csvToBeanConverter.iterator();

bookIter.forEachRemaining(book -> {
    System.out.println("book: " + book);
});

UPDATE 1:
I start to think that the two "standard" solutions OpenCSV and apache-commons-csv both have their weaknesses, so feel free to tell me your favorite CSV libraries. Put them into comments, not answers, because it's opinionated information by definition.

UPDATE 2:
How to read CSV headers and get them in to a list in java is only a workaround-grade solution for this question, because for making it work together with CsvToBean it involves mark() and reset() on the BufferedReader and therefor it limits the possible header size, throwing an exception if exceeded. See my answer for for details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无言温柔 2025-02-16 15:00:15

作为解决方法,您可以在创建csvtobean之前使用header = csvreader.readnext()。但是,您必须mark()reset() bufferedReader,以便可以通过csvtobean再次读取标题信息课程以进行映射。
请注意,这将允许的标头的大小限制为bufferedReader的缓冲区大小,并在reset()中抛出异常,如果超过此大小。

这是代码:

BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream()));

CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();

CSVReader csvReader = new CSVReaderBuilder(in).withCSVParser(csvParser).build();

in.mark(8192);
String[] header = csvReader.readNext();
in.reset();
System.out.println("header: " + Arrays.toString(header));

CsvToBean<Book> csvToBeanConverter = new CsvToBeanBuilder<Book>(csvReader).withType(Book.class).build();

Iterator<Book> bookIter = csvToBeanConverter.iterator();

bookIter.forEachRemaining(book -> {
    System.out.println("book: " + book);
});

As a workaround you can use header = csvReader.readNext() before creating the CsvToBean. However, you have to mark() and reset() the BufferedReader so that the header information can be read again by the CsvToBean class in order to do its mapping.
Note that this limits the size of the allowed header to the buffer size of the BufferedReader and throws an exception in reset() if this size is exceeded.

Here is the code:

BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream()));

CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();

CSVReader csvReader = new CSVReaderBuilder(in).withCSVParser(csvParser).build();

in.mark(8192);
String[] header = csvReader.readNext();
in.reset();
System.out.println("header: " + Arrays.toString(header));

CsvToBean<Book> csvToBeanConverter = new CsvToBeanBuilder<Book>(csvReader).withType(Book.class).build();

Iterator<Book> bookIter = csvToBeanConverter.iterator();

bookIter.forEachRemaining(book -> {
    System.out.println("book: " + book);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文