Example usage for org.apache.commons.csv CSVFormat TDF

List of usage examples for org.apache.commons.csv CSVFormat TDF

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat TDF.

Prototype

CSVFormat TDF

To view the source code for org.apache.commons.csv CSVFormat TDF.

Click Source Link

Document

Tab-delimited format.

Usage

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;

        try {/* w  w w  .  j  a v a 2  s .  c o  m*/
            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.ibm.watson.developer_cloud.professor_languo.model.stack_exchange.CorpusBuilder.java

/**
 * get the csv printer if it has been created , create a new one if the csv printer doesn't exist.
 * The reason of doing so is to allow multiple duplicate threads to be serialized to the same TSV
 * file without overwriting each other/*from ww w  .j  a v  a2s.  c o  m*/
 * 
 * @param tsvDir - the folder containing the TSV file
 * @return the csv printer used to write to the TSV file
 * @throws IngestionException
 */
public CSVPrinter getCsvPrinter(String tsvDir) throws IngestionException {
    if (csvPrinter == null) {
        try {
            String tsvFilePath = tsvDir + StackExchangeConstants.DUP_THREAD_TSV_FILE_NAME
                    + StackExchangeConstants.DUP_THREAD_TSV_FILE_EXTENSION;
            File csvFile = new File(tsvFilePath);
            if (csvFile.getParentFile() != null)
                csvFile.getParentFile().mkdirs();
            if (!csvFile.exists())
                csvFile.createNewFile();

            PrintWriter writer = new PrintWriter(new FileWriter(tsvFilePath, true));
            csvPrinter = new CSVPrinter(writer, CSVFormat.TDF.withHeader(getTsvColumnHeaders()));
        } catch (IOException e) {
            throw new IngestionException(e);
        }
        return csvPrinter;
    }
    return csvPrinter;
}

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;

        try {/* w ww.jav  a2  s .c  o m*/
            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.ibm.watson.developer_cloud.professor_languo.model.stack_exchange.CorpusBuilderTest.java

private void deserialiezd_duplicate_threads_should_match_original_duplicate_threads()
        throws IngestionException {

    String csvFilePath = dupCorpusBuilder.getDupThreadDirPath()
            + StackExchangeConstants.DUP_THREAD_TSV_FILE_NAME
            + StackExchangeConstants.DUP_THREAD_TSV_FILE_EXTENSION;
    File csvData = new File(csvFilePath);

    CSVParser parser;/*from w w  w.java2 s .com*/
    List<CSVRecord> records;
    try {
        parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.TDF.withHeader());
        records = parser.getRecords();
    } catch (IOException e) {
        throw new IngestionException(e);
    }

    Set<StackExchangeThread> dupThreadSet = dupCorpusBuilder.getDupThreadSetFromBinFiles();
    for (StackExchangeThread thread : dupThreadSet) {
        String binfileName = dupCorpusBuilder.getDupThreadDirPath() + thread.getId()
                + StackExchangeConstants.BIN_FILE_SUFFIX;
        CSVRecord matchRecord = null;
        for (CSVRecord record : records)
            if (Integer.parseInt(record.get(0)) == thread.getId()) {
                matchRecord = record;
                break;
            }
        assertTrue(matchRecord != null);
        // TODO haven't check the originId yet since it requires the new
        // method to get origin id from
        String deserTitle = matchRecord.get(1), deserBody = matchRecord.get(2),
                deserFileName = matchRecord.get(4), deserTags = matchRecord.get(5);
        assertEquals(deserTitle, thread.getQuestion().getTitle());
        assertEquals(deserBody, thread.getQuestion().getBody());
        assertEquals(deserFileName, binfileName);
        assertEquals(deserTags, thread.getConcatenatedTagsText());
    }
}

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;

        try {//  w  w w  .  j a  v  a  2s .  co m
            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;

        try {/*from w  ww .ja v a 2  s  . c o  m*/
            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.model.Dataset.java

/**
 * Attempt to load a dataset from a file. We use regular expressions to try to guess the file type,
 * and then delegate the job out. Rather cleverly, we try extracting the names using every extractor
 * this project knows about, and then pick the one that gives us the most number of names.
 * /*from  w  w w.  j  a  va  2s .  co m*/
 * @param proj The project doing the loading, used to get the name extractors.
 * @param f The file to open.
 * @return The dataset loaded from that file.
 * @throws IOException If there was an error loading the file.
 */
public static Dataset loadFromFile(Project proj, File f) throws IOException {
    Dataset ds;

    // Excel file? Handle separately!
    String fileName = f.getName().toLowerCase();
    if (fileName.endsWith(".xlsx") || fileName.endsWith(".xls")) {
        ds = new ExcelImporter(f).asDataset(0);
    } else if (fileName.endsWith(".csv") || fileName.endsWith(".tsv")) {
        CSVFormat csvFormat = CSVFormat.DEFAULT;
        if (fileName.endsWith(".tsv"))
            csvFormat = CSVFormat.TDF.withQuote(null); // We need this to load the AmphibiaWeb files.

        ds = Dataset.fromCSV(csvFormat, f);
    } else {
        // Text-based file? Try using the first line to figure out what's going on.
        String firstLine;
        try (LineNumberReader r = new LineNumberReader(new FileReader(f))) {
            // Load the first line to try to identify the file type.
            firstLine = r.readLine();
        }

        // The most basic type of file is a TaxDiff file, which always
        // begins with:
        if (ChecklistDiff.pTaxDiffFirstLine.matcher(firstLine).matches()) {
            // Note that checklist diffs don't need name extractors!
            return ChecklistDiff.fromTaxDiffFile(f);
        }

        // If all else fails, try loading it as a checklist. Also don't need name extractors!
        return Checklist.fromListInFile(f);
    }

    // If we're here, we need name extractors.

    // Try all name extractors, see which one matches the most names.
    Set<List<NameExtractor>> allAvailableNameExtractors = proj.getNameExtractors();
    allAvailableNameExtractors.add(NameExtractorFactory.getDefaultExtractors());

    LOGGER.info("Starting name extractor comparisons");
    List<NameExtractor> bestExtractor = null;
    long bestExtractorCount = Long.MIN_VALUE;
    for (List<NameExtractor> extractor : allAvailableNameExtractors) {
        long count = ds.rows.stream()
                .flatMap(row -> NameExtractorFactory.extractNamesUsingExtractors(extractor, row).stream())
                .distinct().count();

        if (count > bestExtractorCount) {
            bestExtractorCount = count;
            bestExtractor = extractor;
        }
    }
    LOGGER.info("Finished name extractor comparisons: best extractor at " + bestExtractorCount + " names was "
            + NameExtractorFactory.serializeExtractorsToString(bestExtractor));

    try {
        ds.setNameExtractorsString(NameExtractorFactory.serializeExtractorsToString(bestExtractor));
    } catch (NameExtractorParseException ex) {
        // Forget about it. We'll go with the default.
    }

    return ds;
}

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;

        try {//from w  w w . j  a  va2 s.co  m
            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:no.packdrill.android.sparkledroid.lib.parse.csvtsv.TSVParse.java

@Override
public void parse(Reader input) throws IOException
//------------------------------------------------
{
    CSVFormat format = CSVFormat.TDF.withHeader().withCommentStart('%');
    parser = new CSVParser(input, format);
    headerMap = parser.getHeaderMap();/*from w ww.j a  v  a2 s.  co  m*/
    Set<Map.Entry<String, Integer>> ss = headerMap.entrySet();
    columns = new String[headerMap.size()];
    for (Map.Entry<String, Integer> e : ss)
        columns[e.getValue()] = e.getKey();
    it = parser.iterator();
}

From source file:org.apache.logging.log4j.core.layout.CsvLogEventLayoutTest.java

@Test
public void testLayoutTab() throws Exception {
    testLayout(CSVFormat.TDF);
}