我正在制作一个简单的程序,它从csv获取原始数据并将其放在一个对象上。但我对这个错误有点混淆(似乎当天的格式是错误的)。
控制台出错:
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[file [C:\Users\juan.m.palazzolo\Desktop\data.csv]], input=[Perez,Juan,9/10/1992,123,FALSE]
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:83)
at com.mistaiker.model.RunApp.main(RunApp.java:31)
Caused by: java.lang.IllegalArgumentException: Unparseable date: "9/10/1992", format: [yyyy-MM-dd]
at org.springframework.batch.item.file.transform.DefaultFieldSet.parseDate(DefaultFieldSet.java:778)
at org.springframework.batch.item.file.transform.DefaultFieldSet.readDate(DefaultFieldSet.java:533)
at com.mistaiker.model.ClientFieldSetMapper.mapFieldSet(ClientFieldSetMapper.java:15)
at com.mistaiker.model.ClientFieldSetMapper.mapFieldSet(ClientFieldSetMapper.java:1)
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43)
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
... 2 more
这是我要填写的文章(Client.java)
package com.mistaiker.model;
import java.util.Date;
public class Client {
private String firstName;
private String lastName;
private Date birthday;
private long id;
private boolean active;
void showData(){
System.out.println("FirstName=" + firstName + "," +
"LastName=" + lastName + "," +
"Birthday=" + birthday + "," +
"Id=" + id + "," +
"Active=" + active);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
这是我负责解析数据的类(ClientFieldSetMapper)
package com.mistaiker.model;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class ClientFieldSetMapper implements FieldSetMapper<Client>{
public Client mapFieldSet(FieldSet fieldSet) throws BindException {
Client client = new Client();
client.setFirstName(fieldSet.readRawString(0));
client.setLastName(fieldSet.readRawString(1));
client.setBirthday(fieldSet.readDate(2));
client.setId(fieldSet.readLong(3));
client.setActive(fieldSet.readBoolean(4));
return null;
}
}
这是我正在运行的应用程序(RunApp.java)
package com.mistaiker.model;
import java.util.ArrayList;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
public class RunApp {
public static void main(String[] args) {
FlatFileItemReader<Client> itemReader = new FlatFileItemReader<Client>();
itemReader.setResource(new FileSystemResource("C:/Users/juan.m.palazzolo/Desktop/data.csv"));
DefaultLineMapper<Client> lineMapper = new DefaultLineMapper<Client>();
lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
lineMapper.setFieldSetMapper(new ClientFieldSetMapper());
itemReader.setLineMapper(lineMapper);
itemReader.open(new ExecutionContext());
Client client = new Client();
ArrayList<Client> clients = new ArrayList<Client>();
try {
while((client = itemReader.read()) != null){
client.showData();
clients.add(client);
}
} catch (UnexpectedInputException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Client x: clients){
x.showData();
}
}
}
最后这里是我的数据文件:
Perez,Juan,9/10/1992,123,FALSE
Perez,Augusto,1/10/1993,123,TRUE
Perez,Nalina,3/10/1993,123,TRUE
Gonzales,Raul,6/10/1992,123,FALSE
Gonzales,Josue,9/10/1992,123,TRUE
Carter,Steve,9/10/1992,123,TRUE
非常感谢您阅读n.n
答案 0 :(得分:2)
在 ClientFieldSetMapper 中使用的FieldSet的readDate()以指定格式读取日期 - [yyyy-MM-dd]
。即使在例外情况下,它也会说java.lang.IllegalArgumentException: Unparseable date: "9/10/1992", format: [yyyy-MM-dd]
使用fieldSet.readDate(index, pattern)
解决此问题。
在您的情况下,模式应为MM/dd/yyyy
或dd/MM/yyyy
有关详情,请访问documentation