私が正しくあなたの問題を理解していれば、あなたはこれを処理し、マップ内のデータを取得するためにuniVocity-parsersを使用することができます。
//First create a configuration object - there are many options
//available and the tutorial has a lot of examples
CsvParserSettings settings = new CsvParserSettings();
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
parser.beginParsing(new File("/path/to/your.csv"));
// you can also apply some transformations:
// NULL year should become 0000
parser.getRecordMetadata().setDefaultValueOfColumns("0000", "Year");
// decimal separator in prices will be replaced by comma
parser.getRecordMetadata().convertFields(Conversions.replace("\\.00", ",00")).set("Price");
Record record;
while ((record = parser.parseNextRecord()) != null) {
Map<String, String> map = record.toFieldMap(/*you can pass a list of column names of interest here*/);
//for performance, you can also reuse the map and call record.fillFieldMap(map);
}
それともでもファイルを解析し、単一段階でさまざまな種類の豆を得ることができます。ここではあなたがそれを行う方法は次のとおりです。あなたのデータが大きすぎると、あなたが使用して行で入力行をストリーミングすることができ、メモリ内のすべてを保持することができない場合
CsvParserSettings settings = new CsvParserSettings();
//Create a row processor to process input rows. In this case we want
//multiple instances of different classes:
MultiBeanListProcessor processor = new MultiBeanListProcessor(TestBean.class, AmountBean.class, QuantityBean.class);
// we also need to grab the headers from our input file
settings.setHeaderExtractionEnabled(true);
// configure the parser to use the MultiBeanProcessor
settings.setRowProcessor(processor);
// create the parser and run
CsvParser parser = new CsvParser(settings);
parser.parse(new File("/path/to/your.csv"));
// get the beans:
List<TestBean> testBeans = processor.getBeans(TestBean.class);
List<AmountBean> amountBeans = processor.getBeans(AmountBean.class);
List<QuantityBean> quantityBeans = processor.getBeans(QuantityBean.class);
は例hereとhere
を参照してください。代わりにMultiBeanRowProcessorメソッドrowProcessed(Map<Class<?>, Object> row, ParsingContext context)
は、現在の行の各クラスに対して作成されたインスタンスのマップを提供します。メソッド内では、
AmountBean amountBean = (AmountBean) row.get(AmountBean.class);
QuantityBean quantityBean = (QuantityBean) row.get(QuantityBean.class);
...
//perform something with the instances parsed in a row.
を呼び出すだけです。
免責事項:私はこのライブラリの作者です。それはオープンソースでフリーです(Apache 2.0ライセンス)
POJOではなくマップを使用することを検討しましたか? –