简体   繁体   English

如何构建openCSV CSVReader

[英]How to construct openCSV CSVReader

Sorry for asking such a noob question.很抱歉问了这样一个菜鸟问题。 I've tried multiple things.我已经尝试了很多东西。 All the examples I see online are what I tried in (1) and it isn't working.我在网上看到的所有示例都是我在 (1) 中尝试过的,但它不起作用。

String source= "C:\\temp\\data.csv"; CSVReader csvreader= new CSVReader(new FileReader(csv),","); 
  1. The above code gives the error "Constuctor CSVReader(FileReader, String) is not defined", but I've seen this used online.上面的代码给出了错误“构造函数 CSVReader(FileReader, String) is not defined”,但我在网上看到过这个。 I think it is deprecated but can't I still use it?我认为它已被弃用,但我仍然不能使用它吗? I checked the documentation and tried to use the constructor shown there in (2).我检查了文档并尝试使用(2)中显示的构造函数。

  2. String source = "C:\temp\data.csv";字符串源 = "C:\temp\data.csv";
    Reader reader = new FileReader(source);阅读器 reader = new FileReader(source); CSVReader csvreader= new CSVReader(reader); CSVReader csvreader=新的CSVReader(阅读器);

Gives the error:给出错误:

> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/apache/commons/lang3/ObjectUtils  at
> com.opencsv.CSVParser.<init>(CSVParser.java:99)   at
> com.opencsv.CSVReader.<init>(CSVReader.java:99)   at
> csvpractice.main(csvpractice.java:54) Caused by:
> java.lang.ClassNotFoundException: org.apache.commons.lang3.ObjectUtils
>   at
> java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
>   at
> java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
>   at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
>   ... 3 more
  1. "Deprecated" means that you should not use it because it is liable to be removed in a later version. “已弃用”表示您不应该使用它,因为它可能会在以后的版本中被删除。 You are probably using such a later version, where it has been removed - so you REALLY cannot use it.您可能正在使用这样一个较新的版本,它已被删除 - 所以您真的不能使用它。
    The current API shows that it has been removed: http://opencsv.sourceforge.net/apidocs/com/opencsv/CSVReader.html当前的API显示已经被删除: http://opencsv.sourceforge.net/apidocs/com/opencsv/CSVReader.html

  2. String source = "C:\temp\data.csv";

You have single-backslashes here, which means source will be " C:<tab>empdata.csv ".您在这里有单反斜杠,这意味着source将是“ C:<tab>empdata.csv ”。 Replace this by:将其替换为:

String source = "C:\\temp\\data.csv";`
  1. You are missing the jar file that contains ObjectUtils.您缺少包含 ObjectUtils 的 jar 文件。 You can download it from: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.0您可以从以下位置下载: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.0

A better approach would be to use a build tool like Maven or Gradle, which will automatically import required dependencies.更好的方法是使用 Maven 或 Gradle 之类的构建工具,它会自动导入所需的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM