List of usage examples for org.apache.commons.csv CSVParser close
@Override public void close() throws IOException
From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java
/** * Create a header map to the CSV file, but imposes naming conventions on the column names. * *//* w w w . j av a 2 s . co m*/ private void setVariableAttributes() { VariableAttributes variableAttributes = null; int position = 0; Reader reader = null; CSVParser parser = null; VariableName tempName = null; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, dataFileFormat.withHeader()); if (hasHeader) { Map<String, Integer> csvMap = parser.getHeaderMap(); for (String s : csvMap.keySet()) { variableAttributes = new VariableAttributes(new VariableName(s), new VariableLabel(""), DataType.INTEGER, position); variableAttributeMap.put(variableAttributes.getName(), variableAttributes); position++; } } else { Iterator<CSVRecord> iter = parser.iterator(); CSVRecord csvRecord = iter.next(); for (int i = 0; i < csvRecord.size(); i++) { variableAttributes = new VariableAttributes(new VariableName("v" + (i + 1)), new VariableLabel(""), DataType.INTEGER, position); variableAttributeMap.put(variableAttributes.getName(), variableAttributes); position++; } } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); } catch (IOException ex) { theException = ex; } } }
From source file:com.kdmanalytics.toif.ui.common.AdaptorConfiguration.java
/** * Load configuration data from the specified stream. * //from ww w . j av a 2s . com * @param is * @throws IOException */ private synchronized void load(InputStream is) throws IOException { if (!isEmpty()) { // If there is already data loaded, we want to merge the new data merge(is); } else { InputStreamReader in = null; CSVParser parser = null; try { in = new InputStreamReader(is); CSVFormat format = CSVFormat.EXCEL.withDelimiter(',').withIgnoreEmptyLines(); parser = new CSVParser(in, format); // Set to false once the header is read boolean header = true; // Number of rows we have loaded so far int rcount = data.size(); // Import all new rows for (CSVRecord record : parser) { if (header) { parseHeader(record); header = false; } else { rcount = parseData(record, rcount); } } } finally { if (in != null) { in.close(); } if (parser != null) { parser.close(); } } } }
From source file:com.itemanalysis.jmetrik.data.Scorer.java
private void readScoringFile(String fileName) throws IOException { File f = new File(fileName); CSVParser parser = null; Reader reader = null;//from w ww. j a va2 s . c o m GenericItemScoring itemScoring = null; SpecialDataCodes specialCodes = null; String name = ""; String option = ""; int score = 0; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(f)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); for (CSVRecord csvRecord : parser) { name = csvRecord.get("name"); option = csvRecord.get("option"); score = Integer.parseInt(csvRecord.get("score")); itemScoring = new GenericItemScoring(name); itemScoring.addCategory(option, score); specialCodes = new SpecialDataCodes(); if (csvRecord.isMapped("missing")) specialCodes.setMissingDataCode(csvRecord.get("missing")); if (csvRecord.isMapped("missing score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("missing score"))); if (csvRecord.isMapped("notreached")) specialCodes.setMissingDataCode(csvRecord.get("notreached")); if (csvRecord.isMapped("notreached score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("notreached score"))); if (csvRecord.isMapped("omitted")) specialCodes.setMissingDataCode(csvRecord.get("omitted")); if (csvRecord.isMapped("omitted score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("omitted score"))); scoring.put(name, itemScoring); } } catch (IOException ex) { throw (ex); } finally { parser.close(); reader.close(); } }
From source file:edu.isi.misd.scanner.network.modules.worker.processors.ptr.PrepToResearchProcessor.java
private PrepToResearchResponse analyzeFile(PrepToResearchRequest request, File analysisFile) throws Exception { PrepToResearchResponse response = new PrepToResearchResponse(); Integer requestedOmopConceptID = request.getOmopConceptID(); CSVFormat csvFormat = CSVFormat.newFormat(',').withHeader().withCommentMarker('#').withQuote('"'); CSVParser parser = CSVParser.parse(analysisFile, Charset.defaultCharset(), csvFormat); for (CSVRecord csvRecord : parser) { try {/*from ww w. ja va 2s . com*/ this.validateCSVRecord(csvRecord); // check the ID first, if no match continue Integer omopConceptID = Integer .parseInt(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_ID.toString())); if (!requestedOmopConceptID.equals(omopConceptID)) { continue; } // match found, create response output record if (log.isDebugEnabled()) { log.debug(String.format("Found a match for requested ID %s, record: %s", requestedOmopConceptID, csvRecord.toString())); } PrepToResearchRecord ptrRecord = new PrepToResearchRecord(); ptrRecord.setOmopConceptID(omopConceptID); ptrRecord.setOmopConceptName(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_NAME)); ptrRecord.setCategory(csvRecord.get(ExpectedColumnName.CATEGORY)); ptrRecord.setCategoryValue(csvRecord.get(ExpectedColumnName.CATEGORY_VALUE)); ptrRecord.setCountFemales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_FEMALES))); ptrRecord.setCountMales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_MALES))); ptrRecord.setCountTotal(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_TOTAL))); response.getPrepToResearchRecord().add(ptrRecord); } catch (Exception e) { String error = String.format( "An exception occured while processing row number %s with the following values %s: %s", csvRecord.getRecordNumber(), csvRecord.toString(), e.toString()); parser.close(); throw new RuntimeException(error); } } parser.close(); return response; }
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;//w w w .j a v a 2 s . co m while (valuesIt.hasNext()) { input[i] = valuesIt.next(); i++; } parser.close(); return input; }
From source file:br.com.hslife.orcamento.service.ImportacaoLancamentoService.java
@Override public void processarArquivoCSVImportado(Arquivo arquivo, Conta conta) throws ApplicationException { try {/*from w w w. jav a 2 s .c o m*/ // Declarao e leitura dos dados do CSV final Reader reader = new InputStreamReader(new ByteArrayInputStream(arquivo.getDados()), "UTF-8"); final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader()); // Declarao das variveis LancamentoImportado lancamentoImportado = new LancamentoImportado(); for (CSVRecord record : parser) { int quantidade = Integer.parseInt(record.get("QUANTIDADE")); lancamentoImportado.setConta(conta); lancamentoImportado.setData(new SimpleDateFormat("yyyy-MM-dd").parse(record.get("DATA"))); lancamentoImportado.setHistorico(record.get("HISTORICO")); lancamentoImportado.setValor(Double.parseDouble(record.get("VALOR"))); lancamentoImportado.setMoeda(record.get("MOEDA")); lancamentoImportado.setDocumento(record.get("DOCUMENTO")); lancamentoImportado.setObservacao(record.get("OBSERVACAO")); lancamentoImportado.setCategoria(record.get("CATEGORIA")); lancamentoImportado.setFavorecido(record.get("FAVORECIDO")); lancamentoImportado.setMeiopagamento(record.get("MEIOPAGAMENTO")); lancamentoImportado.setHash(lancamentoImportado.getFullLabel()); // Insere o lanamento importado X vezes de acordo com o campo QUANTIDADE for (int i = 1; i <= quantidade; i++) { getRepository().save(lancamentoImportado.clonarLancamento(i)); } } // Fecha os streams parser.close(); reader.close(); } catch (Exception e) { throw new ApplicationException(e); } }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
/** * String Parsing //from w w w . j av a 2 s .c o m */ 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:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java
@Test public void readJmetrikFileTest() { System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file"); CSVParser parser = null; Reader reader = null;/*w w w . j a v a 2s .com*/ 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:com.xoriant.akka.mongodb.bulkimport.actor.FileReaderActor.java
private void readAndInsertCSV(String filePath) { FileReader fileReader = null; CSVParser csvFileParser = null; // Create the CSVFormat object with the header mapping CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(FILE_HEADER_MAPPING); try {/* w w w . jav a2s. co m*/ fileReader = new FileReader(filePath); csvFileParser = new CSVParser(fileReader, csvFileFormat); List<CSVRecord> csvRecords = csvFileParser.getRecords(); CSVRecordBatchMsg csvRecordBatch = new CSVRecordBatchMsg(); boolean batchSent = false; // Skip the header row and start reading CSV records for (int i = 1; i < csvRecords.size(); i++) { CSVRecord record = csvRecords.get(i); BasicDBObject person = new BasicDBObject(); person.put(PERSON_GENDER, record.get(PERSON_GENDER)); person.put(PERSON_TITLE, record.get(PERSON_TITLE)); person.put(PERSON_NAMESET, record.get(PERSON_NAMESET)); person.put(PERSON_SURNAME, record.get(PERSON_SURNAME)); person.put(PERSON_CITY, record.get(PERSON_CITY)); person.put(PERSON_STATE, record.get(PERSON_STATE)); person.put(PERSON_ZIPCODE, record.get(PERSON_ZIPCODE)); csvRecordBatch.add(person); batchSent = false; if (i % batchSize == 0) { batchSentCounter++; csvRecordBatch.setBatchNo(batchSentCounter); mongoInsertionActor.tell(csvRecordBatch, getSelf()); csvRecordBatch = new CSVRecordBatchMsg(); batchSent = true; } } // Last batch maybe pending if there are less than batch size left over records. Sending last batch of such records explicitly if (!batchSent) { batchSentCounter++; csvRecordBatch.setBatchNo(batchSentCounter); mongoInsertionActor.tell(csvRecordBatch, getSelf()); } mongoInsertionActor.tell(new EndOfFileMsg(), getSelf()); System.out.println("FileReaderActor: EOF sent"); } catch (Exception e) { System.out.println("Error in CsvFileReader !!!" + e.getMessage()); } finally { try { fileReader.close(); csvFileParser.close(); } catch (IOException e) { System.out.println("Error while closing fileReader/csvFileParser : " + e.getMessage()); } } }
From source file:GUI.ReadFile.java
public boolean readTrace(String fileName) { FileReader fileReader;/*w ww . jav a2s . co m*/ CSVParser csvFileParser; boolean isSuccess = true; CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(TRACE_HEADER_MAPPING); try { ArrayList<String> Activity_set = new ArrayList<String>(); HashSet<String> ID_set = new HashSet<String>(); traces = new ArrayList<Trace>(); //initialize FileReader object System.out.println(fileName); fileReader = new FileReader(fileName); //initialize CSVParser object csvFileParser = new CSVParser(fileReader, csvFileFormat); //Get a list of CSV file records List<CSVRecord> csvRecords = csvFileParser.getRecords(); Trace t = new Trace(""); //Read the CSV file records starting from the second record to skip the header for (int i = 1; i < csvRecords.size(); i++) { CSVRecord record = csvRecords.get(i); String ID = record.get(CaseID); if (!ID_set.contains(ID) || (i == csvRecords.size() - 1)) { //Discard void trace if (i != 1) { traces.add(t); } ID_set.add(ID); t = new Trace(ID); } Activity ac = new Activity(record.get(Activity), record.get(StartTime), record.get(CompleteTime), record.get(Timestamp)); t.add_activity(ac); if (!Activity_set.contains(ac.get_name())) { Activity_set.add(ac.get_name()); } } //sort activity set by string Collections.sort(Activity_set); //sort trace by ID Collections.sort(traces, new Comparator<Trace>() { @Override public int compare(Trace t1, Trace t2) { return Integer.parseInt(t1.get_ID()) < Integer.parseInt(t2.get_ID()) ? -1 : 1; } }); //Set activity set for each trace for (Trace T : traces) { T.set_ActivitySet((List<String>) Activity_set.clone()); } } catch (Exception e) { System.out.println("Error in CsvFileReader !!!"); e.printStackTrace(); isSuccess = false; return isSuccess; } if (isSuccess) { try { fileReader.close(); csvFileParser.close(); } catch (IOException e) { System.out.println("Error while closing fileReader/csvFileParser !!!"); } } return isSuccess; }