List of usage examples for org.apache.commons.csv CSVParser close
@Override public void close() throws IOException
From source file:de.upb.wdqa.wdvd.revisiontags.TagDownloader.java
/** * Reads the csv file of the TagDownloader *//* ww w . j av a2 s . c o m*/ public static void readFile(File file) { try { logger.info("Starting to read file of TagDownloader ..."); BufferedReader reader = new BufferedReader(new InputStreamReader( new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file))), "UTF-8")); CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180); dataStore.connect(); for (CSVRecord csvRecord : parser) { parseRecord(csvRecord); if (csvRecord.getRecordNumber() % 1000000 == 0) { logger.info("Current Record: " + csvRecord.getRecordNumber()); } } dataStore.disconnect(); parser.close(); logger.info("Tag Distribution:\n" + FrequencyUtils.formatFrequency(tagDistribution)); logger.info("Finished"); } catch (Exception e) { logger.error("", e); } }
From source file:ca.craigthomas.visualclassifier.dataset.DataSetReader.java
/** * Read from a CSV file, and return the samples as a list of doubles. * /* w w w . j a v a 2s . c om*/ * @param filename the name of the file to read from * @return the list of samples from the file * @throws IOException */ public static List<List<Double>> readCSVFile(String filename) throws IOException { File file = new File(filename); String fileContents = FileUtils.readFileToString(file); Reader reader = new StringReader(fileContents); CSVFormat format = CSVFormat.EXCEL; CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records = parser.getRecords(); List<List<Double>> inputs = new ArrayList<List<Double>>(); for (CSVRecord record : records) { List<Double> inputLine = new ArrayList<Double>(); for (int index = 0; index < record.size(); index++) { String value = record.get(index); inputLine.add(Double.parseDouble(value)); } inputs.add(inputLine); } parser.close(); return inputs; }
From source file:net.decix.jatlasx.csv.CsvReader.java
public List<String> getColumn(String rowIdentifier) { CSVParser parser = null; List<String> column = new ArrayList<String>(); try {//w ww.j a v a2 s. c o m parser = new CSVParser(new FileReader(fileName), csvFileFormat); if (parser != null) { for (CSVRecord record : parser) { column.add(record.get(rowIdentifier).toString()); } } parser.close(); } catch (IOException e) { String errorMsg = "Could not read File:" + fileName; System.err.println(e.getClass().getName() + ":" + errorMsg + " (" + this.getClass().getName() + ")"); } return column; }
From source file:com.garethahealy.quotalimitsgenerator.cli.parsers.DefaultCLIParser.java
private Map<String, Pair<Integer, Integer>> parseLines(String instanceTypeCsv) throws IOException, URISyntaxException, ParseException { InputStreamReader inputStreamReader; if (instanceTypeCsv.equalsIgnoreCase("classpath")) { inputStreamReader = new InputStreamReader( getClass().getClassLoader().getResourceAsStream("instancetypes.csv"), Charset.forName("UTF-8")); } else {//w w w .ja va 2 s. c o m URI uri = new URI(instanceTypeCsv); inputStreamReader = new InputStreamReader(new FileInputStream(new File(uri)), Charset.forName("UTF-8")); } CSVParser parser = null; List<CSVRecord> lines = null; try { parser = CSVFormat.DEFAULT.parse(new BufferedReader(inputStreamReader)); lines = parser.getRecords(); } finally { inputStreamReader.close(); if (parser != null) { parser.close(); } } if (lines == null || lines.size() <= 0) { throw new ParseException("instance-type-csv data is empty"); } Map<String, Pair<Integer, Integer>> linesMap = new HashMap<String, Pair<Integer, Integer>>(); for (CSVRecord current : lines) { linesMap.put(current.get(1), new ImmutablePair<Integer, Integer>(Integer.parseInt(current.get(2)), Integer.parseInt(current.get(3)))); } return linesMap; }
From source file:com.fbartnitzek.tasteemall.data.csv.CsvFileReader.java
/** * reads CSV file in data and headers with same count, uses CSV_Format RFC4180 (, and "") * @param file file to read//from w w w . jav a 2 s.c om * @param headers expected columns * @return data */ public static List<List<String>> readCsvFileHeadingAndData(File file, List<String> headers) { List<List<String>> data = new ArrayList<>(); CSVParser csvParser = null; Reader csvReader = null; try { csvReader = new FileReader(file); csvParser = new CSVParser(csvReader, CsvFileWriter.CSV_FORMAT_RFC4180.withHeader()); Map<String, Integer> headerMap = csvParser.getHeaderMap(); // print headerMap for (Map.Entry<String, Integer> entry : headerMap.entrySet()) { System.out.println(entry.getValue() + ": " + entry.getKey()); } // should be same order! // 0 columns seems impossible, but valid // ordered columns instead unordered set (for insert)! headers.addAll(headerMap.keySet()); for (CSVRecord record : csvParser) { List<String> dataEntry = new ArrayList<>(); for (int i = 0; i < headers.size(); ++i) { if (i < record.size()) { dataEntry.add(record.get(i)); } // dataEntry.add(record.get(headers.get(i))); } data.add(dataEntry); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (csvReader != null) { csvReader.close(); } if (csvParser != null) { csvParser.close(); } } catch (IOException e) { // System.out.println("Error while closing fileReader/csvFileParser !!!"); e.printStackTrace(); } } return data; }
From source file:com.cotrino.langnet.GenerateVisualization.java
private void getLanguages(String file) throws IOException { amountWordsPerLanguage = new HashMap<String, Integer>(); Reader reader = new FileReader(file); CSVParser parser = new CSVParser(reader, csvFormat); for (CSVRecord record : parser) { String language = record.get("Language"); int words = Integer.parseInt(record.get("Words")); amountWordsPerLanguage.put(language, words); }/*from w ww . jav a2s . c o m*/ parser.close(); reader.close(); }
From source file:javalibs.CSVExtractor.java
private void readCSV() { try {/*w ww. ja v a2 s. co m*/ CSVParser parser = new CSVParser(Files.newBufferedReader(Paths.get(this.inCSV)), CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim()); // Get all headers Map<String, Integer> rawHeaders = parser.getHeaderMap(); // Store the inRecords this.inRecords = parser.getRecords(); parser.close(); orderHeaders(rawHeaders); } catch (IOException e) { log_.die(e); } }
From source file:com.miovision.oss.awsbillingtools.parser.DetailedLineItemParser.java
@Override public Stream<DetailedLineItem> parse(Reader reader) throws IOException { final CSVParser csvParser = CSV_FORMAT.parse(reader); try {/* ww w . ja v a 2 s.c o m*/ final Iterator<CSVRecord> iterator = csvParser.iterator(); final List<String> tags = readTags(iterator); return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) .map(csvRecord -> createDetailedLineItem(csvRecord, tags)).onClose(() -> { try { csvParser.close(); } catch (IOException e) { throw new RuntimeException(e); } }); } catch (Exception e) { csvParser.close(); throw e; } }
From source file:com.ibm.util.merge.directive.provider.ProviderCsv.java
/** * Retrieve the data (superclass HTTP Provider) and parse the CSV data * @param cf// w w w.j a va 2 s. c o m */ @Override public void getData(MergeContext rtc) throws MergeException { // Get the data super.getData(rtc); DataTable newTable = new DataTable(); CSVParser parser; try { parser = new CSVParser(new StringReader(getFetchedData()), CSVFormat.EXCEL.withHeader()); for (String colName : parser.getHeaderMap().keySet()) { newTable.addCol(colName); } for (CSVRecord record : parser) { ArrayList<String> row = newTable.addNewRow(); for (String field : record) { row.add(field); } } parser.close(); } catch (IOException e) { throw new MergeException(this, e, "CSV Parser Stringreader IO Exception", getFetchedData()); } if (newTable.size() > 0) { getTables().add(newTable); } }
From source file:citation_prediction.CitationCore.java
/** * Get CSV citation history from a file. * //w w w . ja va 2s .c om * @param filename The filename and path containing the citation data. * @param format The format of the file. * @param hasHeader Does the file have a line with headings? * @return A record containing the csv information. */ private static List<CSVRecord> getCSVData(String filename, CSVFormat format, boolean hasHeader) { boolean error = true; List<CSVRecord> list_ourdata = null; try { FileReader ourdata = new FileReader(filename); CSVParser data_parser = new CSVParser(ourdata, format); list_ourdata = data_parser.getRecords(); if (hasHeader) { list_ourdata.remove(0); } //remove header file. Iterator<CSVRecord> list_iterator = list_ourdata.iterator(); for (int rowIndex = 0; rowIndex < list_ourdata.size(); rowIndex++) { CSVRecord record = list_iterator.next(); System.out.println("#" + (rowIndex + 1) + " " + record.toString()); } data_parser.close(); ourdata.close(); error = false; } catch (java.io.FileNotFoundException e) { System.err.println("ERROR: There was an error opening, reading, or parsing the input file."); System.err.println("ERROR:" + filename); error = true; } catch (java.io.IOException e) { System.err.println("ERROR: Could not close the parser or the input file."); error = true; } if (error || list_ourdata == null) { System.exit(1); return null; } else { return list_ourdata; } }