How do I use FlatFileItemReader in the spring batch for space-separated items (multiple records on a single line)?


Spring batch FlatFileItemReader supports two files formats by default, Fix length and delimiter separated. e.g

id name email
1  name1  [email protected]
2  name2  [email protected]

So it will consider each line as an item and fields as properties of item (domain) object.

My input file has just ids separated by space or comma as I just need ids (or it could be anything unique for each item like username or email etc) e.g

7,8,9, ...... so on


username1, username2, username3, username4 .. so on

How can I use FlatFileItemReader to consider each id as an each item i.e in short multiple records on single line ? Or is there any other way to achive this ?

You can use your custom LineMapper to implement the logic as per your requirement to separate the items from each line.

Here is the sample code that will return the list of Ids either separated by comma or space or both in each line.

import java.util.ArrayList;
import java.util.List;

import org.springframework.batch.item.file.LineMapper;

public class CustomLineMapper implements LineMapper<List<String>> {

    public List<String> mapLine(String line, int lineNumber) throws Exception {

        List<String> list = new ArrayList<String>();
        for (String s1 : line.split(",")) {
            for (String s2 : s1.trim().split(" ")) {
        return list;