-2

One of the common tasks in Spring Batch it is used for is parsing CSV files, but CSV files come in all form and shapes… and some of them are syntactically incorrect.I am currently try to parse an invalid CSV file: My CSV file data.CSV:

"1;13/4/1992;16/7/2006"
"2;31/5/1993;1/8/2009"
"3;21/4/1990;27/4/2010"

I used this code here. This works for me but I want to use a CSV of my own! any suggestion please?

abdallah
  • 81
  • 1
  • 1
  • 10
  • show me error and data-1.csv. – sh.seo Apr 18 '19 at 16:19
  • You're returning from inside your loop. This code will always process the first line and then return. I assume that's not what you mean to be doing. - you really need to describe the problem you're having if fixing that doesn't solve it. – CryptoFool Apr 18 '19 at 16:43
  • This is most likely a duplicate question addressing basic file reading: https://stackoverflow.com/a/27324399/196869 – Jim Apr 18 '19 at 16:50
  • I updated my code @devdotlog you can see the file – abdallah Apr 19 '19 at 07:33

1 Answers1

1

As I said in the comment of the answer your linked, you can use a FlatFileItemReader instead of the ListItemReader and it should work. I used the ListItemReader in order to provide a self-contained example you can run (without the need to upload a csv file, etc). The point of the other question was about how to trim the leading/trailing " before parsing the lines, and the trick was to use a composite item processor as shown in the example.

You don't need to create a new reader, here is an example of a FlatFileItemReader you can use with the same example:

@Bean
public FlatFileItemReader<String> itemReader() {
    return new FlatFileItemReaderBuilder<String>()
            .name("itemReader")
            .resource(new FileSystemResource("/absolute/path/to/file"))
            .lineMapper(new PassThroughLineMapper())
            .build();
}

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50