OpenCSV如果我有' |'作为','

发布于 2025-01-23 00:40:33 字数 948 浏览 2 评论 0 原文

我的代码是:

CSVReader reader = new CSVReaderBuilder(new FileReader("C://Users//himanshurai//eclipse-workspace//nike.csv")).withSeparator('|').withSkipLines(1).build();
List<TShirt> tShirtList = reader.readAll().stream().map(data -> {
    //TShirt tShirt = new TShirt(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
    TShirt tShirt = new TShirt();
    tShirt.setId(data[0]);
    tShirt.setName(data[1]);
    tShirt.setColor(data[2]);
    tShirt.setGender(data[3]);
    tShirt.setSize(data[4]);
    tShirt.setPrice(data[5]);
    tShirt.setRating(data[6]);
    tShirt.setIsAvailable(data[7]);
    return tShirt;
}).collect(Collectors.toList());
tShirtList.forEach(System.out::println);

这是我的CSV文件:

​我认为这是因为CSV文件具有'|'作为一个分离器,而不是“”,我尝试使用withperator('|')方法,但它显示出错误的错误。还有其他方法吗?

My code is this:

CSVReader reader = new CSVReaderBuilder(new FileReader("C://Users//himanshurai//eclipse-workspace//nike.csv")).withSeparator('|').withSkipLines(1).build();
List<TShirt> tShirtList = reader.readAll().stream().map(data -> {
    //TShirt tShirt = new TShirt(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
    TShirt tShirt = new TShirt();
    tShirt.setId(data[0]);
    tShirt.setName(data[1]);
    tShirt.setColor(data[2]);
    tShirt.setGender(data[3]);
    tShirt.setSize(data[4]);
    tShirt.setPrice(data[5]);
    tShirt.setRating(data[6]);
    tShirt.setIsAvailable(data[7]);
    return tShirt;
}).collect(Collectors.toList());
tShirtList.forEach(System.out::println);

And this is my CSV file:

enter image description here

My code is showing an Index out of bound error. I think it is because the CSV file has '|' as a separator instead of ',' and I tried to use withSeperator('|') method but it is showing error like the method is undefined. Is there any other way to do it?

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

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

发布评论

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

评论(1

我爱人 2025-01-30 00:40:33

好的。因此,除了原始的拼写错误外,我认为您正在获得 csvparserbuilder csvReaderBuilder 类混合。

根据,您应该这样使用它们:

final CSVParser parser =
    new CSVParserBuilder()
   .withSeparator('\t')
   .withIgnoreQuotations(true)
   .build();
final CSVReader reader =
    new CSVReaderBuilder(new StringReader(csv))
   .withSkipLines(1)
   .withCSVParser(parser)
   .build();

请注意, withSeparator 方法在 csvparserbuilder not csvReaderBuilder

(当我仔细看...我发现在此不匹配源代码。

OK. So, in addition to the original spelling error, I think you are getting the CSVParserBuilder and CSVReaderBuilder classes mixed up.

According to the source code, you should be using them like this:

final CSVParser parser =
    new CSVParserBuilder()
   .withSeparator('\t')
   .withIgnoreQuotations(true)
   .build();
final CSVReader reader =
    new CSVReaderBuilder(new StringReader(csv))
   .withSkipLines(1)
   .withCSVParser(parser)
   .build();

Notice that the withSeparator method is on CSVParserBuilder not CSVReaderBuilder.

(When I looked carefully ... the javadoc that I found here doesn't match the source code. It shows withSeparator in the example, but not in the method list. Go figure.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文