我有一个tsv
文件,它有固定的行但每行都映射到不同的Java类。
例如。
recordType recordValue1
recordType recordValue1 recordValue2
for first row我有以下课程:
public class FirstRow implements ItsvRecord {
@Parsed(index = 0)
private String recordType;
@Parsed(index = 1)
private String recordValue1;
public FirstRow() {
}
}
对于第二行我有:
public class SecondRow implements ItsvRecord {
@Parsed(index = 0)
private String recordType;
@Parsed(index = 1)
private String recordValue1;
public SecondRow() {
}
}
我想直接将TSV文件解析为相应的对象,但我没有想法。
答案 0 :(得分:1)
使用InputValueSwitch
。这将匹配每行特定列中的值,以确定要使用的RowProcessor
。例如:
final BeanListProcessor<FirstRow> firstProcessor = new BeanListProcessor<FirstRow>(FirstRow.class);
final BeanListProcessor<SecondRow> secondProcessor = new BeanListProcessor<SecondRow>(SecondRow.class);
InputValueSwitch
://0 means that the first column of each row has a value that
//identifies what is the type of record you are dealing with
InputValueSwitch valueSwitch = new InputValueSwitch(0);
//assigns the first processor to rows whose first column contain the 'firstRowType' value
valueSwitch.addSwitchForValue("firstRowType", firstProcessor);
//assigns the second processor to rows whose first column contain the 'secondRowType' value
valueSwitch.addSwitchForValue("secondRowType", secondProcessor);
TsvParserSettings settings = new TsvParserSettings(); //configure...
// your row processor is the switch
settings.setProcessor(valueSwitch);
TsvParser parser = new TsvParser(settings);
Reader input = new StringReader(""+
"firstRowType\trecordValue1\n" +
"secondRowType\trecordValue1\trecordValue2");
parser.parse(input);
List<FirstRow> firstTypeObjects = firstProcessor.getBeans();
List<SecondRow> secondTypeObjects = secondProcessor.getBeans();
[FirstRow{recordType='firstRowType', recordValue1='recordValue1'}]
[SecondRow{recordType='secondRowType', recordValue1='recordValue1', recordValue2='recordValue2'}]
如果您的FirstRow
应包含为SecondRow
类型的记录解析的元素,只需覆盖rowProcessorSwitched
方法:
InputValueSwitch valueSwitch = new InputValueSwitch(0) {
@Override
public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
if (from == secondProcessor) {
List<FirstRow> firstRows = firstProcessor.getBeans();
FirstRow mostRecentRow = firstRows.get(firstRows.size() - 1);
mostRecentRow.addRowsOfOtherType(secondProcessor.getBeans());
secondProcessor.getBeans().clear();
}
}
};
FirstRow
班级有一个addRowsOfOtherType
方法,该方法会将SecondRow
列为参数。您甚至可以混合搭配其他类型的RowProcessor
。还有另一个例子here来证明这一点。
希望这有帮助。