List of usage examples for org.apache.commons.csv CSVParser CSVParser
public CSVParser(final Reader reader, final CSVFormat format) throws IOException
If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .
From source file:Client.Message.java
public Message(String message) throws IOException { this.message = message; Reader in = new StringReader(message); CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT); List<CSVRecord> list = parser.getRecords(); type = MessageType.valueOf(list.get(0).toString()); author = list.get(1).toString();/*from w ww. ja va 2 s. c om*/ if (type == MessageType.TEXT) { text = list.get(2).toString(); } }
From source file:biz.ganttproject.impex.csv.CsvReaderImpl.java
CsvReaderImpl(InputStream is, CSVFormat format) throws IOException { myParser = new CSVParser(new InputStreamReader(is, Charsets.UTF_8), format); }
From source file:assignment.CSVFileReader.java
@Override public List<Map<String, String>> readFile(String filePath) { Reader reader;/*w ww . java 2 s . c o m*/ List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT); Iterator<CSVRecord> csvRecord = csvParser.iterator(); CSVRecord headers = csvRecord.next(); for (CSVRecord row : csvParser) { Map<String, String> item = new HashMap<String, String>(); int colNr = 0; for (String header : headers) { String r = ""; try { r = row.get(colNr); } catch (Exception ex) { } item.put(header, r); colNr++; } rows.add(item); } } catch (Exception ex) { } return rows; }
From source file:de.dhbw.vetaraus.CSV.java
/** * Create a list of Case objects from a given filepath. The filepath must be a CSV file with semicolon-delimited * entries. The first line of the file (header record) will be ignored. * <p>/* ww w .j a va2s . co m*/ * Each line of the file must have the following values: * ID, age, gender, married, children, degree, occupation, income, tariff. * * @param path * Path to the CSV file. * @return a list of Case objects from the given input file. * @throws IOException */ public static List<Case> parse(String path) throws IOException { try (FileInputStream fis = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(fis); BufferedReader file = new BufferedReader(isr)) { final CSVParser parser = new CSVParser(file, CSVFormat.DEFAULT.withDelimiter(';') .withHeader(Constants.HEADER_NUMBER, Constants.HEADER_AGE, Constants.HEADER_GENDER, Constants.HEADER_MARRIED, Constants.HEADER_CHILDREN, Constants.HEADER_DEGREE, Constants.HEADER_OCCUPATION, Constants.HEADER_INCOME, Constants.HEADER_TARIFF) .withSkipHeaderRecord(true)); return parser.getRecords().stream() .map(record -> new Case(record.get(Constants.HEADER_NUMBER), record.get(Constants.HEADER_AGE), record.get(Constants.HEADER_GENDER), record.get(Constants.HEADER_MARRIED), record.get(Constants.HEADER_CHILDREN), record.get(Constants.HEADER_DEGREE), record.get(Constants.HEADER_OCCUPATION), record.get(Constants.HEADER_INCOME), record.get(Constants.HEADER_TARIFF))) .collect(Collectors.toList()); } }
From source file:loternik.MyService.java
public String getHelloMessage(String teryt, String okreg) { FileReader file = null;// www . j a va 2 s.c om try { String output = ""; file = new FileReader("kandydaci.csv"); CSVParser parser = new CSVParser(file, CSVFormat.EXCEL.withHeader().withDelimiter(';')); for (CSVRecord csvRecord : parser) { if (teryt.equals(csvRecord.get("teryt")) && okreg.equals(csvRecord.get("Okreg"))) { output += "<p>" + csvRecord.toString() + "</p>"; } } return output; } catch (FileNotFoundException ex) { Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex); } finally { try { file.close(); } catch (IOException ex) { Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex); } } return null; }
From source file:br.edimarmanica.trinity.check.CheckNrPages.java
private int nrPagesExtracted() { Set<String> pages = new HashSet<>(); File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset"); if (!dir.exists()) { return 0; }//from w w w . j a va2 s . co m for (File offset : dir.listFiles()) { try (Reader in = new FileReader(offset)) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) { for (CSVRecord record : parser) { pages.add(record.get(0)); } } } catch (FileNotFoundException ex) { Logger.getLogger(CheckNrPages.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CheckNrPages.class.getName()).log(Level.SEVERE, null, ex); } } return pages.size(); }
From source file:br.edimarmanica.trinity.check.CheckOffsetRule.java
public void execute() { File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset"); List<List<String>> offset = new ArrayList<>(); //cada arquivo um offset try (Reader in = new FileReader(dir.getAbsoluteFile() + "/result_" + indexOffset + ".csv")) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) { int nrRegistro = 0; for (CSVRecord record : parser) { if (nrRegistro >= nrRegistros) { break; }//ww w .ja v a 2 s. c om System.out.println(record.get(0) + ";" + record.get(indexRule)); nrRegistro++; } } } catch (FileNotFoundException ex) { Logger.getLogger(CheckOffsetRule.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CheckOffsetRule.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:convertCSV.ConvertCSV.java
/** * @param fichier_/* w w w . j a va2 s .c o m*/ * @throws java.io.IOException */ public void importer(String fichier_) throws IOException { // TODO code application logic here float lat, lon, ele, secjour; int bpm; List<MonPoint> points = new ArrayList<>(); Reader in = null; CSVParser parser; List<CSVRecord> list; GPXWriter monGPX = new GPXWriter(); // lecture du CSV try { System.out.println("Lecture de " + fichier_); in = new FileReader(fichier_); } catch (FileNotFoundException ex) { Logger.getLogger(ConvertCSV.class.getName()).log(Level.SEVERE, null, ex); } parser = new CSVParser(in, CSVFormat.EXCEL); list = parser.getRecords(); list.remove(0); // remplissage de la liste de point GPX if (in != null) { for (CSVRecord elem : list) { try { // on recupere les donnees dans le CSV lat = Float.parseFloat(elem.get(0)); lon = Float.parseFloat(elem.get(1)); ele = Float.parseFloat(elem.get(2)); secjour = Float.parseFloat(elem.get(3)); if (elem.size() > 4) { bpm = Integer.parseInt(elem.get(4)); points.add(new MonPoint(lat, lon, ele, secjour, bpm)); } else { points.add(new MonPoint(lat, lon, ele, secjour)); } } catch (NumberFormatException ex) { System.out.println(elem.toString()); } } // ecriture du GPX monGPX.writePath("C:\\Users\\vincent\\Desktop\\today.gpx", "Training", points); in.close(); } System.exit(0); }
From source file:com.example.sihan.restaurantrecommendation.Function.FileResource.java
public CSVParser getCSVParser(boolean withHeader, String delimiter) { if (delimiter == null || delimiter.length() != 1) { throw new ResourceException("FileResource: CSV delimiter must be a single character: " + delimiter); }/* ww w . j a va 2 s . c o m*/ try { char delim = delimiter.charAt(0); Reader input = new StringReader(mySource); if (withHeader) { return new CSVParser(input, CSVFormat.EXCEL.withHeader().withDelimiter(delim)); } else { return new CSVParser(input, CSVFormat.EXCEL.withDelimiter(delim)); } } catch (Exception e) { throw new ResourceException("FileResource: cannot read " + myPath + " as a CSV file."); } }
From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingRouteDetailsDirectory.java
private static void parseRoutesFile(final Store<RouteIdKey, RouteDetails> store, final Reader routeReader) throws InterruptedException, IOException { final CSVParser routeParser = new CSVParser(routeReader, CSVFormat.DEFAULT.withHeader()); final List<CSVRecord> routeRecords = routeParser.getRecords(); for (final CSVRecord record : routeRecords) { String routeId = record.get("route_id"); String routeShortName = record.get("route_short_name"); String routeLongName = record.get("route_long_name"); populateRouteDetail(routeId, routeShortName, routeLongName, store); }// w ww . j ava 2s.c om }