如何使用OpenCSV从输入CSV文件中提取标头?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为解决方法,您可以在创建
csvtobean
之前使用header = csvreader.readnext()
。但是,您必须mark()
和reset()
bufferedReader
,以便可以通过csvtobean再次读取标题信息
课程以进行映射。请注意,这将允许的标头的大小限制为
bufferedReader
的缓冲区大小,并在reset()
中抛出异常,如果超过此大小。这是代码:
As a workaround you can use
header = csvReader.readNext()
before creating theCsvToBean
. However, you have tomark()
andreset()
theBufferedReader
so that the header information can be read again by theCsvToBean
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 inreset()
if this size is exceeded.Here is the code: