我正在尝试使用OpenCSV将一些数据写入CSV文件。但是,当我尝试存储数据时,我会得到CsvBeanIntrospectionException
,然后是NoSuchMethodException: Unknown property 'Email' on class 'class SubscriberBean'
。
这是我的JavaBean类的代码:
import com.opencsv.bean.CsvBindByName;
import java.io.Serializable;
public class SubscriberBean implements Serializable {
@CsvBindByName
String Email;
@CsvBindByName
String Name;
public SubscriberBean() {}
public SubscriberBean(String email, String name) {
this.Email = email;
this.Name = name;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
@Override
public String toString() {
return "SubscriberBean [" + "Email=" + Email + ", Name=" + Name + ']';
}
public Boolean isEmpty() {
return (this.Name.equals("") || this.Email.equals("") || this.Name == null || this.Email == null);
}
以及我如何尝试在我的csv文件中存储数据的代码:
List<SubscriberBean> lsb = new ArrayList<SubscriberBean>();
.
.
.
try {
Writer writer = new FileWriter("test.csv");
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
beanToCsv.write(lsb);
writer.close();
} catch (CsvDataTypeMismatchException e) {
e.printStackTrace();
} catch (CsvRequiredFieldEmptyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我检查了JavaBean类,看起来很好。 CSV代码与opencsv网站上提供的示例几乎相同。
答案 0 :(得分:2)
几乎在那里;)
apache beanutils期望您的字段以小写字母开头。例如。 email
,而非Email
等。这是固定的SubscriberBean
:
public class SubscriberBean implements Serializable {
@CsvBindByName
String email;
@CsvBindByName
String name;
public SubscriberBean() {
}
public SubscriberBean(String email, String name) {
this.email = email;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// ...
}
请注意,由于小写字段,我不得不稍微更改setter。我添加了this
引用:this.email = email;
答案 1 :(得分:0)
由于吸气剂和吸气剂,我也有同样的例外。还应该定义它们