简体   繁体   English

使用OpenCSV读取文件

[英]File reading using OpenCSV

I use OpenCSV for CSV files reading and writing. 我将OpenCSV用于读取和写入CSV文件。 It worked fine until I hsd to install earlier version of Java - ie Java 6. After installing it, my code stopped working: 直到我hsd安装Java的早期版本(即Java 6)之前,它都工作正常。安装完之后,我的代码停止工作:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/opencsv/CSVReader : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) 线程“主”中的异常java.lang.UnsupportedClassVersionError:com / opencsv / CSVReader:java.lang.ClassLoader.defineClass1(本机方法)处的java.lang.ClassLoader.defineClass1(本机方法)不受支持的major.minor版本51.0(java处) java.security.SecureClassLoader.defineClass上的.lang.ClassLoader.defineClass(未知源)

在此处输入图片说明

在此处输入图片说明

Check the version of the class file in your OpenCSV .jar dependency, for example: 检查您的OpenCSV .jar依赖项中的类文件的版本,例如:

javap -verbose -cp opencsv.jar com.opencsv.CSVReader | grep major

If this shows a version newer than 50 (= newer than Java 6) then you cannot use this jar. 如果显示的版本高于50(=比Java 6更高),则不能使用此jar。 You need to get a .jar file built with Java 6 or older. 您需要获取使用Java 6或更早版本构建的.jar文件。 (If it can be built with Java 6 at all.) (如果可以完全用Java 6构建。)

I just confirmed that the latest version of the source code doesn't compile with Java 6, as it uses Java 7 features (naturally). 我刚刚确认源代码的最新版本无法与Java 6一起编译,因为它自然使用了Java 7功能。 But not too long ago, as of August 2014 the project was still on Java 5. So if you download an older version of the jar it should work. 但是不久前,截至2014年8月,该项目仍在Java 5上。因此,如果您下载较旧版本的jar,它应该可以工作。

In other words, since you downgraded your Java version, you also need to downgrade your OpenCSV dependency. 换句话说,由于降级了Java版本,因此还需要降级OpenCSV依赖项。

UPDATE UPDATE

From SourceForge , version 2.3 contains a jar file that should be suitable for you: SourceForge中 ,版本2.3包含一个应该适合您的jar文件:

$ javap -verbose -cp deploy/opencsv-2.3.jar au.com.bytecode.opencsv.CSVReader | grep major
  major version: 50

Note that the package name is a bit different from the current version. 请注意,程序包名称与当前版本略有不同。

When OpenCSV was upgraded to 3.0 the decision was made to upgrade to Java 7. This was done for several reasons. 当OpenCSV升级到3.0时,决定升级到Java7。这样做的原因有很多。 The main reason being that the largest reason was that the main fixes in 3.0 was to support the lazy checks in Lambdas that are in Grails (possibly Java 8) so it was requested the code be compiled in Java 7 because it is more optimal for Grails. 主要原因是最大的原因是3.0版中的主要修补程序是为了支持Grails(可能是Java 8)中Lambdas中的惰性检查,因此要求在Java 7中编译代码,因为它对Grails来说是最佳选择。 I allowed that because Oracle had announced it was dropping support for Java 7 in 2015 and Java 5 & 6 had long since been unsupported plus I had a small number of emails asking when I was going to port opencsv to java 7 (and only one for java 8 for some reason) so I felt that moving to java 7 was safe. 我同意了,因为甲骨文已经宣布它将在2015年放弃对Java 7的支持,而Java 5和6早就没有得到支持,而且我收到的电子邮件很少,询问何时将opencsv移植到Java 7(只有一个用于Java 8(出于某种原因),所以我觉得迁移到Java 7是安全的。 The last was that I develop using a mac and no longer have access to a java 5 compiler and I really did not feel good about pushing out a Java 5 version of opencsv built using java 7. 最后一个是我使用Mac开发的,无法再使用Java 5编译器,因此我对推出使用Java 7构建的Java 5版本的opencsv真的不满意。

In hindsight seeing the issues that this caused (you are the third that I know of that cannot upgrade because of this) I would have left it at Java 5 and asked the Grails users to perform a performance test and myself build opencsv in a Ubuntu VM on my mac. 在事后看来,这是导致问题的原因(您是我所知道的第三个因此而无法升级的问题),我将其留在Java 5上并要求Grails用户执行性能测试,然后自己在Ubuntu VM中构建opencsv在我的Mac上。 But since it is out I don't want to downgrade and possibly effect users as well. 但是既然淘汰了,我就不想降级,也不会影响用户。

If you cannot upgrade your project to java 7 you do have two options: the first is that on the sourceforge and maven repositories you can download the older versions. 如果您不能将项目升级到Java 7,则有两个选择:第一个是在sourceforge和maven存储库上,您可以下载较旧的版本。 So 2.4 and earlier was built using java 5. The other option is that you can download the code and build it yourself. 因此,使用Java 5构建了2.4及更早版本。另一个选择是,您可以下载代码并自己构建。 Thanks to KommradHomer the current code base is compatible with Java 6 and will build as is with a Java 6 compiler (you just need to change the pom file). 多亏了KommradHomer,当前的代码库与Java 6兼容,并且可以像使用Java 6编译器一样进行构建(您只需要更改pom文件)。

I am seeing the same issue. 我看到了同样的问题。 Please let me know which version of openCSV I can use to be compatible with java version "1.6.0_22" ? 请让我知道我可以使用哪个版本的openCSV与Java版本“ 1.6.0_22”兼容? OpenCSV 2.3 does not work for me. OpenCSV 2.3对我不起作用。

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

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