List of usage examples for org.apache.commons.csv CSVRecord iterator
@Override
public Iterator<String> iterator()
From source file:com.marklogic.contentpump.DelimitedTextReader.java
protected String[] getLine() throws IOException { CSVRecord record = (CSVRecord) parserIterator.next(); Iterator<String> recordIterator = record.iterator(); int recordSize = record.size(); String[] values = new String[recordSize]; for (int i = 0; i < recordSize; i++) { if (recordIterator.hasNext()) { values[i] = (String) recordIterator.next(); } else {/*from w ww . ja v a2 s.c o m*/ throw new IOException("Record size doesn't match the real size"); } } return values; }
From source file:canreg.client.gui.components.PreviewFilePanel.java
/** * *//*w w w .j a v a2s. c om*/ @Action public void previewAction() { // show the contents of the file BufferedReader br = null; try { changeFile(); // numberOfRecordsTextField.setText(""+(canreg.common.Tools.numberOfLinesInFile(inFile.getAbsolutePath())-1)); FileInputStream fis = new FileInputStream(inFile); br = new BufferedReader(new InputStreamReader(fis, (Charset) charsetsComboBox.getSelectedItem())); CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(getSeparator()); CSVParser csvParser = new CSVParser(br, csvFormat); int linesToRead = Globals.NUMBER_OF_LINES_IN_IMPORT_PREVIEW; int numberOfLinesRead = 0; Vector<Vector<String>> data = new Vector<Vector<String>>(); String[] headers = csvParser.getHeaderMap().keySet().toArray(new String[0]); for (CSVRecord csvRecord : csvParser) { csvRecord.toMap(); Vector vec = new Vector(); Iterator<String> iterator = csvRecord.iterator(); while (iterator.hasNext()) { vec.add(iterator.next()); } data.add(vec); numberOfLinesRead++; if (numberOfLinesRead >= linesToRead) { break; } } numberOfRecordsShownTextField.setText(numberOfLinesRead + ""); // previewTextArea.setText(headers + "\n" + dataText); // previewTextArea.setCaretPosition(0); previewPanel.setVisible(true); Vector columnNames = new Vector(Arrays.asList(headers)); previewTable.setModel(new DefaultTableModel(data, columnNames)); } catch (FileNotFoundException fileNotFoundException) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, fileNotFoundException); } catch (IOException ex) { Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { Logger.getLogger(PreviewFilePanel.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java
@Test public void readJmetrikFileTest() { System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file"); CSVParser parser = null;/*from w w w . ja v a 2 s. com*/ Reader reader = null; try { File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik")); reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#')); Iterator<CSVRecord> iter = parser.iterator(); CSVRecord temp = null; boolean readAttributes = false; boolean readData = false; int attCount = 0; while (iter.hasNext()) { temp = iter.next(); if ("VERSION".equals(temp.getComment())) { System.out.println("VERSION: " + temp.get(0)); } else if ("METADATA".equals(temp.getComment())) { System.out.println("CASES: " + temp.get(0)); } else if ("ATTRIBUTES".equals(temp.getComment())) { readAttributes = true; } else if ("DATA".equals(temp.getComment())) { readAttributes = false; readData = true; } if (readAttributes) { System.out.print("ATTRIBUTE-" + attCount + ": "); Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); attCount++; } if (readData) { Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { parser.close(); reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:net.sourceforge.ganttproject.io.GanttCSVOpen.java
/** * Create tasks from file./*from w w w . j a v a 2s .co m*/ * * @throws IOException * on parse error or input read-failure */ public boolean load() throws IOException { CSVParser parser = new CSVParser(myInputSupplier.get(), CSVFormat.DEFAULT.withEmptyLinesIgnored(false).withSurroundingSpacesIgnored(true)); int numGroup = 0; RecordGroup currentGroup = null; boolean searchHeader = true; List<CSVRecord> records = parser.getRecords(); for (CSVRecord record : records) { if (record.size() == 0) { // If line is empty then current record group is probably finished. // Let's search for the next group header. searchHeader = true; continue; } if (searchHeader) { // Record is not empty and we're searching for header. if (numGroup < myRecordGroups.size() && myRecordGroups.get(numGroup).isHeader(record)) { // If next group acknowledges the header, then we give it the turn, // otherwise it was just an empty line in the current group searchHeader = false; currentGroup = myRecordGroups.get(numGroup); parser.readHeader(record); currentGroup.setHeader(Lists.newArrayList(record.iterator())); numGroup++; continue; } searchHeader = false; } assert currentGroup != null; currentGroup.process(record); } for (RecordGroup group : myRecordGroups) { group.postProcess(); } // Succeeded return true; }
From source file:canreg.client.gui.dataentry.ImportView.java
/** * */// w w w . ja va 2 s . c o m @Action public void previewAction() { // show the contents of the file BufferedReader br = null; try { changeFile(); // numberOfRecordsTextField.setText(""+(canreg.common.Tools.numberOfLinesInFile(inFile.getAbsolutePath())-1)); FileInputStream fis = new FileInputStream(inFile); br = new BufferedReader(new InputStreamReader(fis, (Charset) charsetsComboBox.getSelectedItem())); CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(getSeparator()); CSVParser csvParser = new CSVParser(br, csvFormat); int linesToRead = Globals.NUMBER_OF_LINES_IN_IMPORT_PREVIEW; int numberOfLinesRead = 0; Vector<Vector<String>> data = new Vector<Vector<String>>(); String[] headers = csvParser.getHeaderMap().keySet().toArray(new String[0]); for (CSVRecord csvRecord : csvParser) { csvRecord.toMap(); Vector vec = new Vector(); Iterator<String> iterator = csvRecord.iterator(); while (iterator.hasNext()) { vec.add(iterator.next()); } data.add(vec); numberOfLinesRead++; if (numberOfLinesRead >= linesToRead) { break; } } numberOfRecordsShownTextField.setText(numberOfLinesRead + ""); // previewTextArea.setText(headers + "\n" + dataText); // previewTextArea.setCaretPosition(0); previewPanel.setVisible(true); Vector columnNames = new Vector(Arrays.asList(headers)); previewTable.setModel(new DefaultTableModel(data, columnNames)); } catch (FileNotFoundException fileNotFoundException) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, fileNotFoundException); } catch (IOException ex) { JOptionPane.showInternalMessageDialog(CanRegClientApp.getApplication().getMainFrame().getContentPane(), java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("COULD_NOT_PREVIEW_FILE:") + " \'" + fileNameTextField.getText().trim() + "\'.", java.util.ResourceBundle.getBundle("canreg/client/gui/dataentry/resources/ImportView") .getString("ERROR"), JOptionPane.ERROR_MESSAGE); Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { Logger.getLogger(ImportView.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
public static String[] splitStr(String val) throws IOException { CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT); CSVRecord record = parser.getRecords().get(0); Iterator<String> valuesIt = record.iterator(); String[] input = new String[record.size()]; int i = 0;// ww w .jav a2 s .co m while (valuesIt.hasNext()) { input[i] = valuesIt.next(); i++; } parser.close(); return input; }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
/** * String Parsing /* ww w . j av a2s . com*/ */ public static String[] splitStr(String val, Integer len) throws IOException { String[] input; try { CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT); CSVRecord record = parser.getRecords().get(0); input = new String[len]; Iterator<String> valuesIt = record.iterator(); int i = 0; while (valuesIt.hasNext()) { input[i] = valuesIt.next().trim(); i++; } parser.close(); } catch (ArrayIndexOutOfBoundsException e) { input = val.split(",", len); for (int i = 0; i < input.length; i++) input[i] = input[i].trim(); } return input; }
From source file:org.apache.jena.atlas.csv.CSVParser.java
private static List<String> recordToList(CSVRecord record) { return Iter.toList(record.iterator()); }
From source file:org.openlmis.fulfillment.Resource2Db.java
Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException { XLOGGER.entry(resource.getDescription()); // parse CSV/*from ww w .j a v a 2s. co m*/ try (InputStreamReader isReader = new InputStreamReader( new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) { CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader); // read header row MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>(); readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet())); XLOGGER.info("Read header: " + readData.getLeft()); // read data rows List<Object[]> rows = new ArrayList<>(); for (CSVRecord record : parser.getRecords()) { if (!record.isConsistent()) { throw new IllegalArgumentException("CSV record inconsistent: " + record); } List theRow = IteratorUtils.toList(record.iterator()); rows.add(theRow.toArray()); } readData.setRight(rows); XLOGGER.exit("Records read: " + readData.getRight().size()); return readData; } }
From source file:org.shareok.data.documentProcessor.CsvHandler.java
/** * Reads out the data in an excel file and stores data in a hashmap * <p>Also sets the total record number and file heading</p> * // w ww. j a v a2 s. co m * @throws Exception */ @Override public void readData() { FileReader fileReader = null; CSVParser csvFileParser = null; String[] headMapping = null; //CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING); try { //initialize FileReader object fileReader = new FileReader(fileName); //initialize CSVParser object if (null == csvFormat) { csvFormat = CSVFormat.DEFAULT; } csvFileParser = new CSVParser(fileReader, csvFormat); //Get a list of CSV file records List csvRecords = csvFileParser.getRecords(); int size = csvRecords.size(); setRecordCount(size); data = new HashMap(); //Read the CSV file records starting from the second record to skip the header for (int i = 0; i < size; i++) { CSVRecord record = (CSVRecord) csvRecords.get(i); if (null != record) { if (i == 0) { List headMappingList = new ArrayList(); Iterator it = record.iterator(); while (it.hasNext()) { String value = (String) it.next(); headMappingList.add(value); } headMapping = new String[headMappingList.size()]; headMapping = (String[]) headMappingList.toArray(headMapping); setFileHeadMapping(headMapping); } else { for (int j = 0; j < fileHeadMapping.length; j++) { String colName = fileHeadMapping[j].trim(); String key = colName + "-" + i; data.put(key, record.get(j)); } } } } } catch (Exception e) { System.out.println("Error in CsvFileReader !!!"); e.printStackTrace(); } finally { try { fileReader.close(); csvFileParser.close(); } catch (IOException e) { System.out.println("Error while closing fileReader/csvFileParser !!!"); e.printStackTrace(); } } }