List of usage examples for org.apache.commons.csv CSVFormat RFC4180
CSVFormat RFC4180
To view the source code for org.apache.commons.csv CSVFormat RFC4180.
Click Source Link
From source file:de.tudarmstadt.ukp.clarin.webanno.monitoring.page.AgreementTable.java
private InputStream generateCsvReport(AgreementResult aResult) throws UnsupportedEncodingException, IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(buf, "UTF-8"), CSVFormat.RFC4180)) { AgreementUtils.toCSV(printer, aResult); }//from w w w .j a v a2 s. c o m return new ByteArrayInputStream(buf.toByteArray()); }
From source file:dk.dma.ais.abnormal.analyzer.analysis.FreeFlowAnalysis.java
private void writeToCSVFile(FreeFlowData freeFlowData) { if (csvFileName == null) return;/*from w w w . j ava 2s. co m*/ final File csvFile = new File(csvFileName); final boolean fileExists = csvFile.exists() == true || csvFile.length() > 0; lock.lock(); if (!fileExists) { try { if (csvFilePrinter != null) csvFilePrinter.close(); if (fileWriter != null) fileWriter.close(); } catch (IOException e) { LOG.warn(e.getMessage(), e); } csvFilePrinter = null; fileWriter = null; } try { if (fileWriter == null) { try { fileWriter = new FileWriter(csvFile, true); if (csvFilePrinter == null) { try { csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.RFC4180.withCommentMarker('#')); } catch (IOException e) { csvFilePrinter = null; LOG.error(e.getMessage(), e); LOG.error("Failed to write line to CSV file: " + freeFlowData); return; } } } catch (IOException e) { fileWriter = null; LOG.error(e.getMessage(), e); LOG.error("Failed to write line to CSV file: " + freeFlowData); return; } } if (!fileExists) { LOG.info("Created new CSV file: " + csvFile.getAbsolutePath()); csvFilePrinter.printComment("Generated by AIS Abnormal Behaviour Analyzer"); csvFilePrinter.printComment("File created: " + fmt.print(new Date().getTime())); csvFilePrinter.printRecord("TIMESTAMP (GMT)", "MMSI1", "NAME1", "TP1", "LOA1", "BM1", "COG1", "HDG1", "SOG1", "LAT1", "LON1", "MMSI2", "NAME2", "TP2", "LOA2", "BM2", "COG2", "HDG2", "SOG2", "LAT2", "LON2", "BRG", "DST"); } final Track t0 = freeFlowData.getTrackSnapshot(); final Position p0 = freeFlowData.getTrackCenterPosition(); List<FreeFlowData.TrackInsideEllipse> tracks = freeFlowData.getTracksInsideEllipse(); for (FreeFlowData.TrackInsideEllipse track : tracks) { final Track t1 = track.getTrackSnapshot(); final Position p1 = track.getTrackCenterPosition(); final int d = (int) p0.distanceTo(p1, CoordinateSystem.CARTESIAN); final int b = (int) p0.rhumbLineBearingTo(p1); List csvRecord = new ArrayList<>(); csvRecord.add(String.format(Locale.ENGLISH, "%s", fmt.print(t0.getTimeOfLastPositionReport()))); csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getMmsi())); csvRecord.add( String.format(Locale.ENGLISH, "%s", trimAisString(t0.getShipName()).replace(',', ' '))); csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getShipType())); csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getVesselLength())); csvRecord.add(String.format(Locale.ENGLISH, "%d", t0.getVesselBeam())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getCourseOverGround())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getTrueHeading())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t0.getSpeedOverGround())); csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p0.getLatitude())); csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p0.getLongitude())); csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getMmsi())); csvRecord.add( String.format(Locale.ENGLISH, "%s", trimAisString(t1.getShipName()).replace(',', ' '))); csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getShipType())); csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getVesselLength())); csvRecord.add(String.format(Locale.ENGLISH, "%d", t1.getVesselBeam())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getCourseOverGround())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getTrueHeading())); csvRecord.add(String.format(Locale.ENGLISH, "%.0f", t1.getSpeedOverGround())); csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p1.getLatitude())); csvRecord.add(String.format(Locale.ENGLISH, "%.4f", p1.getLongitude())); csvRecord.add(String.format(Locale.ENGLISH, "%d", b)); csvRecord.add(String.format(Locale.ENGLISH, "%d", d)); try { csvFilePrinter.printRecord(csvRecord); } catch (IOException e) { LOG.error(e.getMessage(), e); LOG.error("Failed to write line to CSV file: " + freeFlowData); } } csvFilePrinter.flush(); } catch (IOException e) { LOG.error(e.getMessage(), e); } finally { lock.unlock(); } }
From source file:com.ggvaidya.scinames.ui.BulkChangeEditorController.java
@FXML private void exportToCSV(ActionEvent evt) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter("CSV file", "*.csv"), new FileChooser.ExtensionFilter("Tab-delimited file", "*.txt")); File file = chooser.showSaveDialog(bulkChangeEditor.getStage()); if (file != null) { CSVFormat format = CSVFormat.RFC4180; String outputFormat = chooser.getSelectedExtensionFilter().getDescription(); if (outputFormat.equalsIgnoreCase("Tab-delimited file")) format = CSVFormat.TDF;//from www . jav a2s. c om try { List<List<String>> dataAsTable = getDataAsTable(); fillCSVFormat(format, new FileWriter(file), dataAsTable); Alert window = new Alert(Alert.AlertType.CONFIRMATION, "CSV file '" + file + "' saved with " + (dataAsTable.get(0).size() - 1) + " rows."); window.showAndWait(); } catch (IOException e) { Alert window = new Alert(Alert.AlertType.ERROR, "Could not save CSV to '" + file + "': " + e); window.showAndWait(); } } }
From source file:com.ggvaidya.scinames.dataset.DatasetSceneController.java
private void exportToCSV(TableView tv, ActionEvent evt) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter("CSV file", "*.csv"), new FileChooser.ExtensionFilter("Tab-delimited file", "*.txt")); File file = chooser.showSaveDialog(datasetView.getStage()); if (file != null) { CSVFormat format = CSVFormat.RFC4180; String outputFormat = chooser.getSelectedExtensionFilter().getDescription(); if (outputFormat.equalsIgnoreCase("Tab-delimited file")) format = CSVFormat.TDF;/*w w w . java2s . c o m*/ try { List<List<String>> dataAsTable = getDataAsTable(tv); fillCSVFormat(format, new FileWriter(file), dataAsTable); Alert window = new Alert(Alert.AlertType.CONFIRMATION, "CSV file '" + file + "' saved with " + (dataAsTable.get(0).size() - 1) + " rows."); window.showAndWait(); } catch (IOException e) { Alert window = new Alert(Alert.AlertType.ERROR, "Could not save CSV to '" + file + "': " + e); window.showAndWait(); } } }
From source file:com.ggvaidya.scinames.ui.DatasetDiffController.java
@FXML private void exportToCSV(ActionEvent evt) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter("CSV file", "*.csv"), new FileChooser.ExtensionFilter("Tab-delimited file", "*.txt")); File file = chooser.showSaveDialog(datasetDiffView.getStage()); if (file != null) { CSVFormat format = CSVFormat.RFC4180; String outputFormat = chooser.getSelectedExtensionFilter().getDescription(); if (outputFormat.equalsIgnoreCase("Tab-delimited file")) format = CSVFormat.TDF;//from w w w . j a v a 2 s. c om try { List<List<String>> dataAsTable = getDataAsTable(); fillCSVFormat(format, new FileWriter(file), dataAsTable); Alert window = new Alert(Alert.AlertType.CONFIRMATION, "CSV file '" + file + "' saved with " + (dataAsTable.get(0).size() - 1) + " rows."); window.showAndWait(); } catch (IOException e) { Alert window = new Alert(Alert.AlertType.ERROR, "Could not save CSV to '" + file + "': " + e); window.showAndWait(); } } }
From source file:com.ggvaidya.scinames.ui.DataReconciliatorController.java
@FXML private void exportToCSV(ActionEvent evt) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter("CSV file", "*.csv"), new FileChooser.ExtensionFilter("Tab-delimited file", "*.txt")); File file = chooser.showSaveDialog(dataReconciliatorView.getStage()); if (file != null) { CSVFormat format = CSVFormat.RFC4180; String outputFormat = chooser.getSelectedExtensionFilter().getDescription(); if (outputFormat.equalsIgnoreCase("Tab-delimited file")) format = CSVFormat.TDF;//from ww w .j a va 2 s .co m try { List<List<String>> dataAsTable = getDataAsTable(); fillCSVFormat(format, new FileWriter(file), dataAsTable); Alert window = new Alert(Alert.AlertType.CONFIRMATION, "CSV file '" + file + "' saved with " + (dataAsTable.get(0).size() - 1) + " rows."); window.showAndWait(); } catch (IOException e) { Alert window = new Alert(Alert.AlertType.ERROR, "Could not save CSV to '" + file + "': " + e); window.showAndWait(); } } }
From source file:com.ggvaidya.scinames.dataset.BinomialChangesSceneController.java
private void exportToCSV(TableView tv, ActionEvent evt) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().setAll(new FileChooser.ExtensionFilter("CSV file", "*.csv"), new FileChooser.ExtensionFilter("Tab-delimited file", "*.txt")); File file = chooser.showSaveDialog(binomialChangesView.getStage()); if (file != null) { CSVFormat format = CSVFormat.RFC4180; String outputFormat = chooser.getSelectedExtensionFilter().getDescription(); if (outputFormat.equalsIgnoreCase("Tab-delimited file")) format = CSVFormat.TDF;/*from ww w .j av a 2 s.c o m*/ try { List<List<String>> dataAsTable = getDataAsTable(tv); fillCSVFormat(format, new FileWriter(file), dataAsTable); Alert window = new Alert(Alert.AlertType.CONFIRMATION, "CSV file '" + file + "' saved with " + (dataAsTable.get(0).size() - 1) + " rows."); window.showAndWait(); } catch (IOException e) { Alert window = new Alert(Alert.AlertType.ERROR, "Could not save CSV to '" + file + "': " + e); window.showAndWait(); } } }
From source file:nl.detoren.ijsco.io.OSBOLoader.java
public Spelers laadCSV(String csvpath) { File csvData = new File(csvpath); CSVParser parser = null;//from ww w .ja va 2s.co m try { parser = CSVParser.parse(csvData, java.nio.charset.Charset.defaultCharset(), CSVFormat.RFC4180); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (CSVRecord csvRecord : parser) { //TODO } return null; }
From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java
public void parse(String deathFolder) throws IOException { File f = new File(deathFolder); Runtime runtime = Runtime.getRuntime(); if (f.isDirectory()) { String[] filesInDir = f.list(); for (String fil : filesInDir) { if (fil.endsWith(".zip")) { ZipFile zipFile = new ZipFile(deathFolder + "/" + fil); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { System.out.println( "Used memory: " + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024) + " Free memory: " + (runtime.freeMemory()) / (1024 * 1024)); ZipEntry entry = entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8")); CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader() .parse(br);/* w w w .j ava2 s . co m*/ List<CSVRecord> records = parser.getRecords(); System.out.println("Reading records: " + zipFile.getName() + "/" + entry); /*for (int i=0;i<records.size();i++) { CSVRecord csvRecord = records.get(i);*/ for (CSVRecord csvRecord : records) { if (csvRecord.isMapped("age_group_id")) { //age group 22 corresponds to all ages if (csvRecord.get("age_group_id").equalsIgnoreCase("22")) { String location = null; String year = null; String sex = null; String cause = null; String number = null; String metric = null; if (csvRecord.isMapped("location_code")) { location = csvRecord.get("location_code"); } if (csvRecord.isMapped("year")) { year = csvRecord.get("year"); } if (csvRecord.isMapped("sex_id")) { //1=male, 2 = female if (csvRecord.get("sex_id").equalsIgnoreCase(("1"))) { sex = "m"; } else if (csvRecord.get("sex_id").equalsIgnoreCase("2")) { sex = "f"; } } if (csvRecord.isMapped("cause_name")) { cause = csvRecord.get("cause_name"); } if (csvRecord.isMapped("mean")) { number = csvRecord.get("mean"); } if (csvRecord.isMapped("metric") && csvRecord.isMapped("unit")) { metric = csvRecord.get("metric") + "-" + csvRecord.get("unit"); } if (location != null && year != null && sex != null && cause != null && number != null && metric != null) { try { sqlM.addSanitaryIssueToCountry(location, year, sex, cause, metric, number); } catch (SQLException ex) { Logger.getLogger(CSVsGBDdata.class.getName()).log(Level.SEVERE, null, ex); } } } } } parser.close(); stream.close(); br.close(); } zipFile.close(); } } } else { System.out.println("Not a directory"); } }
From source file:no.uio.medicine.virsurveillance.parsers.CSVVirusNames.java
public ArrayList<String> getVirusNames(String inputVirusFile) throws FileNotFoundException, IOException { ArrayList<String> viruses = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader(inputVirusFile)); CSVParser parser = CSVFormat.RFC4180.withDelimiter(';').withIgnoreEmptyLines().withHeader().parse(reader); for (CSVRecord csvRecord : parser) { if (csvRecord.isMapped("Virus Name")) { String currentVirus = csvRecord.get("Virus Name").replace(" virus", "").replace("", "%%"); viruses.add(currentVirus);/*from w w w. ja v a 2s . c o m*/ } } return viruses; }