如何在spring批处理中读取ini文件(key = value)

我想创建一个批处理使用Spring批处理从ini文件中读取数据并将数据保存在数据库中但是当我查询org.springframework.batch.item.file.FlatFileItemReader类时,我没有找到解析数据的方法在ini文件中,我试图将ini4j API与spring批处理相结合但没有结果

我的ini文件:

  [Cat] a=1 b= 2 c= 3 d= 4 e= 5 f= 6 [Cat2] a=11 b= 21 c= 31 d= 41 e= 51 f= 61 

您可以做的是定义自己的ItemStreamReader ,它包装一个委托ItemStreamReader ,它只是一个使用PatternMatchingCompositeLineMapper作为行映射器的FlatFileItemReader 。 在ItemStreamReader ,循环读取委托中的行,如果该行是Property域对象的实例,则将其添加到Section域对象的列表中。 PatternMatchingCompositeLineMapper允许您执行的操作是检查模式匹配的行,并将其传递给作业的右标记器和fieldSetMapper。

这样做将允许您将多行读入一个包含List Section域对象。

 public class Section { private String name; private List properties; // getters and setters @Override public String toString() { StringBuilder sb = new StringBuilder(name); for (Property prop: properties) { sb.append("," + prop.getKey() + "=" + prop.getValue()); } return sb.toString(); } } public class Property { private String key; private String value; // getters and setters } 

对于自定义ItemStreamReader您可以这样做。 您可以看到读数被委托给另一个读者,您将在稍后定义

 import java.util.ArrayList; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ItemStreamReader; import org.springframework.batch.item.NonTransientResourceException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; public class IniFileItemReader implements ItemStreamReader { private Object curItem = null; private ItemStreamReader delegate; @Override public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { if (curItem == null) { curItem = (Section) delegate.read(); } Section section = (Section) curItem; curItem = null; if (section != null) { section.setProperties(new ArrayList()); while (peek() instanceof Property) { section.getProperties().add((Property) curItem); curItem = null; } } return section; } private Object peek() throws Exception { if (curItem == null) { curItem = delegate.read(); } return curItem; } public void setDelegate(ItemStreamReader delegate) { this.delegate = delegate; } @Override public void close() throws ItemStreamException { delegate.close(); } @Override public void open(ExecutionContext arg0) throws ItemStreamException { delegate.open(arg0); } @Override public void update(ExecutionContext arg0) throws ItemStreamException { delegate.update(arg0); } } 

然后在您的配置中使用PatternMatchingCompositeLineMapper定义deleagte阅读器

                                             

你看,我也使用了自定义LineTozenizer 。 我可能只是使用了DelimitedLineTokenizer ,但是当我意识到它时,我已经定义了这个类

 import org.springframework.batch.item.file.transform.DefaultFieldSetFactory; import org.springframework.batch.item.file.transform.FieldSet; import org.springframework.batch.item.file.transform.FieldSetFactory; import org.springframework.batch.item.file.transform.LineTokenizer; public class SectionLineTokenizer implements LineTokenizer { private final String nameField = "name"; private final FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory(); @Override public FieldSet tokenize(String line) { String name = line.replaceAll("\\[", "").replaceAll("\\]", "").trim(); return fieldSetFactory.create(new String[] { name }, new String[] { nameField }); } } 

使用以下作者和工作

                        

并使用此ini文件

 [Cat] a=1 b=2 c=3 d=4 e=5 f=6 [Cat2] a=11 b=21 c=31 d=41 e=51 f=61 

我得到以下输出,这是Section类的toString()中的格式

 Cat,a=1,b=2,c=3,d=4,e=5,f=6 Cat2,a=11,b=21,c=31,d=41,e=51,f=61