Skip header, body and footer lines from file on Spring Batch

青春壹個敷衍的年華 提交于 2019-12-06 03:07:41

Nope. linesToSkip (as you wrote) just skip the first linesToSkip lines.
You have to write your own reader using multiorder-line example (or this post) as base and manage skip first line of each block manually

Another option would be this one:

1- Create a Reader Factory

public class CustomFileReaderFactory implements BufferedReaderFactory {
@Override
public BufferedReader create(Resource resource, String encoding) throws IOException {
    return new CustomFileReader(new InputStreamReader(resource.getInputStream(), encoding));
}

2- Create your CustomFileReader (this will read one line and decide if we continue or we skip) and make sure to overwrite the readLine() method.

public class CustomFileReader extends BufferedReader {

public CustomFileReader(Reader in) {
    super(in);
}

@Override
public String readLine() throws IOException {
    String line = super.readLine();
    // your logic here
    if (hasToBeIgnored(line)) {
        return null;
    }
    return line;
}

3- Set your brand new Factory into your FlatFileItemReader:

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