使用 Univocity CSV 解析器解析具有相同定义但列数不同的两个不同文件
Using Univocity CSV parser to parse two different files having same definition but varies in the number of column count
我正在使用 Univocity 解析器解析 CSV 文件并将它们填充到 Bean
。
我面临的问题是我有两个不同的文件。这两个文件的结构相同,但列数不同。他们都引用同一个 Bean class.
例如:
File A contains(without header):
I|123|Hello
U|345|Hi
File B contains(without header):
123|Hello
345|Hi
Bean Class 定义是:
public class Bean {
@Trim
@Parsed(index = 0)
protected String action;
@Trim
@Parsed(index = 1)
protected Long id;
@Trim
@Parsed(index = 2)
protected String name;
......................
}
如果我对两个文件使用相同的 bean,则两个文件中的列数应该相同,但它会失败。
我认为我可以使用的另一种方法是为不同的文件集使用两个不同的 bean,但我正在寻找 Univocity 解析器中是否有任何功能来处理这种情况。
请帮忙。谢谢
您可以在解析每个输入之前设置 headers "by hand"。例如:
CsvParserSettings s = new CsvParserSettings();
s.setHeaderExtractionEnabled(false);
CsvRoutines r = new CsvRoutines(s);
//set headers of input with 3 columns
s.setHeaders("action" , "id", "name");
for(Bean b : r.iterate(Bean.class, new StringReader("I,123,Hello\nU,345,Hi"))){
System.out.println(b);
}
//set headers of input with 2 columns
s.setHeaders("id", "name");
for(Bean b : r.iterate(Bean.class, new StringReader("123,Hello\n345,Hi"))){
System.out.println(b);
}
如果您将 bean 更改为使用 header 名称而不是列位置,以上内容将起作用:
public class Bean {
@Trim
@Parsed
protected String action;
@Trim
@Parsed
protected Long id;
@Trim
@Parsed
protected String name;
我得到了以下输出(在 Bean
class 中添加了 toString()
方法之后):
Bean{action='I', id=123, name='Hello'}
Bean{action='U', id=345, name='Hi'}
Bean{action='null', id=123, name='Hello'}
Bean{action='null', id=345, name='Hi'}
希望对您有所帮助!
我正在使用 Univocity 解析器解析 CSV 文件并将它们填充到 Bean
。
我面临的问题是我有两个不同的文件。这两个文件的结构相同,但列数不同。他们都引用同一个 Bean class.
例如:
File A contains(without header):
I|123|Hello
U|345|Hi
File B contains(without header):
123|Hello
345|Hi
Bean Class 定义是:
public class Bean {
@Trim
@Parsed(index = 0)
protected String action;
@Trim
@Parsed(index = 1)
protected Long id;
@Trim
@Parsed(index = 2)
protected String name;
......................
}
如果我对两个文件使用相同的 bean,则两个文件中的列数应该相同,但它会失败。
我认为我可以使用的另一种方法是为不同的文件集使用两个不同的 bean,但我正在寻找 Univocity 解析器中是否有任何功能来处理这种情况。
请帮忙。谢谢
您可以在解析每个输入之前设置 headers "by hand"。例如:
CsvParserSettings s = new CsvParserSettings();
s.setHeaderExtractionEnabled(false);
CsvRoutines r = new CsvRoutines(s);
//set headers of input with 3 columns
s.setHeaders("action" , "id", "name");
for(Bean b : r.iterate(Bean.class, new StringReader("I,123,Hello\nU,345,Hi"))){
System.out.println(b);
}
//set headers of input with 2 columns
s.setHeaders("id", "name");
for(Bean b : r.iterate(Bean.class, new StringReader("123,Hello\n345,Hi"))){
System.out.println(b);
}
如果您将 bean 更改为使用 header 名称而不是列位置,以上内容将起作用:
public class Bean {
@Trim
@Parsed
protected String action;
@Trim
@Parsed
protected Long id;
@Trim
@Parsed
protected String name;
我得到了以下输出(在 Bean
class 中添加了 toString()
方法之后):
Bean{action='I', id=123, name='Hello'}
Bean{action='U', id=345, name='Hi'}
Bean{action='null', id=123, name='Hello'}
Bean{action='null', id=345, name='Hi'}
希望对您有所帮助!