2

我正在制作一个简单的程序,它从 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

嗯,谢谢你的阅读

4

1 回答 1

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/yyyydd/MM/yyyy

有关更多信息 -文档

于 2013-11-14T02:52:38.487 回答