Untitled
unknown
plain_text
3 years ago
1.2 kB
6
Indexable
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.List; public class MyCSVReader { public static Object[][] getValues(String path) { List<CSVRecord> lisOfRecord; CSVParser parser; int rowCount; try { Reader reader = new FileReader(path); parser = new CSVParser(reader, CSVFormat.EXCEL.withFirstRecordAsHeader()); lisOfRecord = parser.getRecords(); rowCount = lisOfRecord.size(); } catch (IOException e) { throw new RuntimeException(e); } Object[][] result = new Object[rowCount][]; List<String> headers = parser.getHeaderNames(); for (int i = 0; i < rowCount; i++) { Object[] recordValue = new Object[headers.size()]; for (int j = 0; j < headers.size(); j++) { recordValue[j] = lisOfRecord.get(i).get(headers.get(j)); } result[i] = recordValue; } return result; } }
Editor is loading...