问题
I have this specifically file:
H;COD;CREATION_DATE;TOT_POR;TYPE
H;001;2013-10-30;20;R
D;DETAIL_VALUE;PROP_VALUE
D;003;3030
D;002;3031
D;005;3032
T;NUM_FOL;TOT
T;1;503.45
As you can see, it has header/body/footer lines. I'm looking for a ItemReader that skip these lines. I've done this ItemReader below who identify those lines, using PatternMatchingCompositeLineMapper.
<bean id="fileReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" ref="myFileReference" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
<property name="tokenizers">
<map>
<entry key="H*" value-ref="headerLineTokenizer"/>
<entry key="D*" value-ref="bodyLineTokenizer"/>
<entry key="T*" value-ref="footerLineTokenizer"/>
</map>
</property>
<property name="fieldSetMappers">
<map>
<entry key="H*" value-ref="headerMapper"/>
<entry key="D*" value-ref="bodyMapper"/>
<entry key="T*" value-ref="footerMapper"/>
</map>
</property>
</bean>
</property>
</bean>
I tried to add linesToSkip property equals 1, but it only skipped the header line. Is there a way to skip the first line of each block(header, body and footer)?
Thks.
回答1:
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
回答2:
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());
来源:https://stackoverflow.com/questions/20956768/skip-header-body-and-footer-lines-from-file-on-spring-batch