Spring Batch : How to parse a horked CSV file?

二次信任 提交于 2019-12-13 11:19:50

问题


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?


回答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.



来源:https://stackoverflow.com/questions/55749966/spring-batch-how-to-parse-a-horked-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!