Я надеюсь, что добрые люди могут помочь мне с моей ситуацией с CSV. Мне нужно только прочитать первые 3 столбца в CSV, который я использую. У меня нет контроля над количеством столбцов в CSV-файле, и нет доступных заголовков. Я пытался использовать частичное чтение с CSVBeanReader (https://super -csv.github.io / super-csv / examples_partial_reading.html ), но продолжаю получать сообщение об ошибке «массив nameMapping и количество столбцов для чтения». Я хотел бы спросить, работает ли пример частичного чтения для supercsv версии 2.4.0, которую я сейчас использую. См. Ниже код, который я использовал, который я создал по образцу, похожему на пример частичного чтения
public class MainPartialRead {
public void partialRead() throws Exception {
ICsvBeanReader beanReader = null;
String csv_filename = "test2.csv";
try {
beanReader = new CsvBeanReader(new FileReader(csv_filename), CsvPreference.STANDARD_PREFERENCE);
beanReader.getHeader(true); // skip past the header (we're defining our own)
System.out.println("beanreader Length: " + beanReader.length());
// only map the first 3 columns - setting header elements to null means those columns are ignored
final String[] header = new String[]{"column1", "column2", "column3", null, null, null, null, null,
null, null};
// no processing required for ignored columns
final CellProcessor[] processors = new CellProcessor[]{new NotNull(), new NotNull(),
new NotNull(), null, null, null, null, null, null, null};
beanCSVReport customer;
while ((customer = beanReader.read(beanCSVReport.class, header, processors)) != null) {
System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(),
beanReader.getRowNumber(), customer));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
}
}
Вот пример файла CSV, который я использую:
466,24127,abc,53516
868,46363,hth,249