跳过Spring批量中的页脚行数

时间:2017-07-24 10:45:39

标签: java spring spring-batch flatfilereader

我使用<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container demo"> <div class="text-center"> <button type="button" class="btn btn-demo" data-toggle="modal" data-target="#myModal"> Left Sidebar Modal </button> <button type="button" class="btn btn-demo" data-toggle="modal" data-target="#myModal2"> Right Sidebar Modal </button> </div> <!-- Modal --> <div class="modal left fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">Left Sidebar</h4> </div> <div class="modal-body"> <p>Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </p> </div> </div><!-- modal-content --> </div><!-- modal-dialog --> </div><!-- modal --> <!-- Modal --> <div class="modal right fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel2">Right Sidebar</h4> </div> <div class="modal-body"> <p>Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </p> </div> </div><!-- modal-content --> </div><!-- modal-dialog --> </div><!-- modal --> </div><!-- container -->来读取分隔的平面文件。虽然我可以跳过字段FlatFileItemReader的标题数量,但我无法按行数跳过页脚。

1 个答案:

答案 0 :(得分:0)

您可以创建自定义行映射器,您可以在其中跳过正则表达式匹配或行号。

public class CustomLineMapper extends DefaultLineMapper<FieldSet> {
    @Setter
    private int totalItemsToRead;
    @Override
    public FieldSet mapLine(String line, int lineNumber) throws Exception {
        if(lineNumber > totalItemsToRead){
            return null;
        }
        return super.mapLine(line, lineNumber);
    }
}

最后注册自定义行映射器FlatFileItemReader

CustomLineMapper lineMapper = new CustomLineMapper();
    lineMapper.setTotalItemsToRead(totalLinesInFile - numberOfLinesToSkipInFooter);

FlatFileItemReader<FieldSet> reader = new FlatFileItemReader<>();
// skip headers
reader.setLinesToSkip(linesToSkipInHeader);
// skip footer
reader.setLineMapper(lineMapper);