我正在尝试使用opencsv 3.3解析一些简单的csv文件并将其放入bean中,但是在运行代码时遇到了一个类未找到的异常。我正在用于读入对象的Person.class文件位于下面。
public class Person {
private String firstname;
private String lastname;
private String email;
private String type;
/*public Person () {}
public Person (String firstname, String lastname, String email, String type) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.type = type;
}*/
//set all the variables for the person
//set the first name
public String getFirstName() {
return firstname;
}
public void setFirstName(String firstname) {
this.firstname = firstname;
}
//set the last name
public String getLastName() {
return lastname;
}
public void setLastName (String lastname) {
this.lastname = lastname;
}
//set the email address of the user
public String setEmail() {
return email;
}
public void getEmail(String email) {
this.email = email;
}
//set the type for the user
public String setType() {
return type;
}
public void getType (String type) {
this.type = type;
}
@Override
public String toString() {
return "User [firstname=" + firstname + ", lastname=" + lastname + ", email=" + email + ", type=" + type + "]";
}
}
阅读csv的代码如下。
String filename = "D:/Projects/deploy/resources/sample.csv";
try {
HeaderColumnNameTranslateMappingStrategy<Person> strategy = new HeaderColumnNameTranslateMappingStrategy<Person>();
strategy.setType(Person.class);
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("firstname", "firstname");
mapping.put("lastname", "lastname");
mapping.put("email", "email");
mapping.put("type", "type");
strategy.setColumnMapping(mapping);
System.out.println("past the mapping");
CSVReader reader = new CSVReader(new FileReader(filename));
System.out.println("past the reader");
CsvToBean<Person> csvbean = new CsvToBean<Person>();
System.out.println("past the csvtobean");
List<Person> csvusers = csvbean.parse(strategy, reader);
System.out.println(csvusers);
} catch (Exception e) {
System.out.println("there was an exception in the main program:" + e);
}
当我运行它时,它表示找不到标题映射的类。你知道这是Person.class或映射的问题吗?我们非常感谢您提供的任何帮助。
答案 0 :(得分:1)
如果有人遇到这个,上面的代码工作正常。我的classpath中没有apache commons lang3,这是opencsv所必需的。一旦我添加了它,一切都开始像它应该的那样工作。
由于 尼克