'Use Spring Batch read csv file

I use spring batch technology to read csv files. This example is provided in spring batch office, but I can't seem to run it. Office link: Simple Delimited File Reading Example

My code is below:

     *  csv 文件读取
     * @return
     */
    @Bean
    public ItemReader<Player> readCsvFileReader() throws Exception {
        FlatFileItemReader<Player> itemReader = new FlatFileItemReader<>();
        itemReader.setResource(new ClassPathResource(generateFilePath));
        DefaultLineMapper<Player> lineMapper = new DefaultLineMapper<>();
        //DelimitedLineTokenizer defaults to comma as its delimiter
        lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
        lineMapper.setFieldSetMapper(new PlayerFieldSetMapper());
        itemReader.setLineMapper(lineMapper);
        // 官网没加,这里是需要的
        itemReader.setLinesToSkip(1);
        return itemReader;
    } 

The csv file content is below:

ID,lastName,firstName,position,birthYear,debutYear
"AbduKa00,Abdul-Jabbar,Karim,rb,1974,1996",
"AbduRa00,Abdullah,Rabih,rb,1975,1999",
"AberWa00,Abercrombie,Walter,rb,1959,1982",
"AbraDa00,Abramowicz,Danny,wr,1945,1967",
"AdamBo00,Adams,Bob,te,1946,1969",
"AdamCh00,Adams,Charlie,wr,1979,2003"

I know this is due to quotes in the example. It normally runs if I remove the quotes. The csv file content is below:

ID,lastName,firstName,position,birthYear,debutYear
AbduKa00,Abdul-Jabbar,Karim,rb,1974,1996,
AbduRa00,Abdullah,Rabih,rb,1975,1999,
AberWa00,Abercrombie,Walter,rb,1959,1982,
AbraDa00,Abramowicz,Danny,wr,1945,1967,
AdamBo00,Adams,Bob,te,1946,1969,
AdamCh00,Adams,Charlie,wr,1979,2003

As far as I know, more the delimiter is ',' and quoteCharacter is '"' about default DelimitedLineTokenizer class.

enter image description here

So I want to know why did the quotes fail after setting up a default DelimitedLineTokenizer. How to parse the file the problem if the csv file exist the prefix of quotes?

The example codes has provided in github, link refer:https://github.com/shenyun499/gather_all_single_function/tree/boot_web_spring_batch_project.

There exists an error after running the main program now.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source