Example usage for org.apache.commons.csv CSVParser close

List of usage examples for org.apache.commons.csv CSVParser close

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes resources.

Usage

From source file:com.ibm.util.merge.directive.provider.ProviderTag.java

/**
 * Reset the table, and if the Tag exists, add a row with the tag name/value
 * @param cf//  w  w  w  . java 2  s . com
 */
@Override
public void getData(MergeContext rtc) throws MergeException {
    reset();
    DataTable table = addNewTable();
    Template template = getDirective().getTemplate();
    String theTag = Template.wrap(tag);
    log.info("Getting Tag Data for " + tag);

    switch (condition) {
    case ProviderTag.CONDITION_EXISTS:
        if (!template.hasReplaceKey(theTag)) {
            log.info("Tag not found for Exists Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_BLANK:
        if (!template.hasReplaceKey(theTag) || template.hasReplaceValue(theTag)) {
            log.info("Tag not found or Data found for Blank Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_NONBLANK:
        if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag)) {
            log.info("Tag or Empty Data found for Non-Blank Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_EQUALS:
        if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag)
                || !template.getReplaceValue(theTag).equals(value)) {
            log.info("Tag not Equals or not found");
            return;
        }
        break;
    }

    // We have a match, so add data
    String data = template.getReplaceValue(Template.wrap(tag));
    log.info("Data Found: " + data);
    table.addCol(tag);
    if (isList()) {
        CSVParser parser;
        try {
            parser = new CSVParser(new StringReader(data), CSVFormat.EXCEL.withHeader());
            for (String colName : parser.getHeaderMap().keySet()) {
                ArrayList<String> row = table.addNewRow();
                row.add(colName);
            }
            parser.close();
        } catch (IOException e) {
            throw new MergeException(this, e, "CSV Parser Stringreader IO Exception", data);
        }
    } else {
        ArrayList<String> row = table.addNewRow();
        row.add(data);
    }
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void toTXT(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".txt");
    List<CSVRecord> records = parser.getRecords();
    CSVRecord record;/*from   w ww .j  av a  2  s  .c  o m*/

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        fout.println(record.get(6));
    }

    fout.close();
    parser.close();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVRadiology.java

public void categorize(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    List<CSVRecord> records = parser.getRecords();
    StringJoiner join;//from   ww  w .  ja v a 2 s .  c o m
    CSVRecord record;

    for (int i = 0; i <= 500; i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        join = new StringJoiner(" ");

        for (int j = 2; j < 7; j++)
            join.add(record.get(j));

        System.out.println(join.toString());
    }

    parser.close();
}

From source file:javalibs.CSVDataNormalizer.java

private void readCSV() {
    try {//  ww w .  j  a  v a2 s. com
        CSVParser parser = new CSVParser(Files.newBufferedReader(Paths.get(this.csvPath)),
                CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim());

        // Get all headers in the CSV file so they can be used later when writing the file
        this.headerMap = parser.getHeaderMap();

        // Add them to the records list for later use
        this.allRecords = parser.getRecords();

        parser.close();

        reverseHeaderMap();
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:apiconnector.TestDataFunctionality.java

@Ignore
@Test/*from www.  j  a va2s. c om*/
public void testGetDataAsCsv() throws Exception {
    //client_read.setVerboseLevel(1);
    Random random = new Random();

    Map<String, String> filters = new TreeMap<String, String>();
    filters.put("tag", "study_14");

    DataSet[] all = client_read.dataList(filters).getData();

    for (int i = 0; i < 5;) {
        DataSet current = all[random.nextInt(all.length)];

        String numInst = current.getQualityMap().get("NumberOfInstances");

        if (current.getFileId() == null || !current.getFormat().toLowerCase().equals("arff")) {
            continue;
        }

        String fullUrl = url + "data/get_csv/" + current.getFileId() + "/" + current.getName() + ".csv";
        System.out.println(fullUrl);
        final URL url = new URL(fullUrl);
        final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
        final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
        try {
            if (numInst != null) {
                int numberOfInstances = (int) Double.parseDouble(numInst);
                assertEquals(parser.getRecords().size(), numberOfInstances);
            }
        } finally {
            parser.close();
            reader.close();
        }

        // important
        i += 1;
    }

}

From source file:com.cotrino.langnet.GenerateVisualization.java

private double[] calculateSimilarity(File logFile) throws IOException {

    int i = 0;//from w w w . j ava  2 s  . c  om
    double total = 0.0;
    Reader reader = new FileReader(logFile);
    CSVParser parser = new CSVParser(reader, csvFormat);
    for (CSVRecord record : parser) {
        try {
            double similarity = Double.parseDouble(record.get("Similarity"));
            total += similarity;
            i++;
        } catch (NumberFormatException e) {
            logger.error("At " + logFile.getName() + ", failed line: " + record.toString());
        }
    }
    parser.close();
    reader.close();
    return new double[] { (total / i), i };

}

From source file:com.cotrino.langnet.GenerateVisualization.java

private void generateLanguages(String summaryFile, String languagesFile) throws IOException {

    HashMap<String, Integer> list = new HashMap<String, Integer>();
    Reader reader = new FileReader(summaryFile);
    CSVParser parser = new CSVParser(reader, csvFormat);
    for (CSVRecord record : parser) {
        String languageA = record.get("LanguageA");
        String languageB = record.get("LanguageB");
        int words = Integer.parseInt(record.get("ExecutedComparisons"));
        list.put(languageA, Math.max(words, list.getOrDefault(languageA, 0)));
        list.put(languageB, Math.max(words, list.getOrDefault(languageB, 0)));
    }// w ww .  j av  a2  s  . co  m
    parser.close();
    reader.close();

    String content = "Language;Words;Family;\n";
    for (String language : list.keySet()) {
        content += language + ";" + list.get(language) + ";Romance;\n";
    }
    IOUtil.write(languagesFile, content);
}

From source file:com.blackducksoftware.integration.hubdiff.HubDiffTest.java

@Test
public void csvTest() throws IOException, IllegalArgumentException, EncryptionException,
        HubIntegrationException, JSONException {
    HubDiff hubDiff = new HubDiff(doc1, doc2);

    hubDiff.writeDiffAsCSV(actualFile);//from  w  ww.  j av a  2  s  . c o  m

    CSVParser expectedParser = new CSVParser(new FileReader(expectedFile), CSVFormat.EXCEL);
    CSVParser actualParser = new CSVParser(new FileReader(actualFile), CSVFormat.EXCEL);
    List<CSVRecord> expectedRecords = expectedParser.getRecords();
    List<CSVRecord> actualRecords = actualParser.getRecords();

    assertEquals(expectedRecords.size(), actualRecords.size());

    for (int i = 0; i < expectedRecords.size(); i++) {
        String expected = expectedRecords.get(i).toString();
        String actual = actualRecords.get(i).toString();
        assertEquals(expected, actual);
    }

    expectedParser.close();
    actualParser.close();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void toTSV(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".tsv");
    List<CSVRecord> records = parser.getRecords();
    List<Token> tokens;/*w  w w. j ava  2  s. c o m*/
    CSVRecord record;
    int label;

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        label = toIntLabel(record.get(0));
        tokens = tokenizer.tokenize(record.get(6));
        fout.println(label + "\t" + Joiner.join(tokens, " ", Token::getWordForm));
    }

    fout.close();
    parser.close();
}

From source file:edu.harvard.mcz.imagecapture.loader.JobVerbatimFieldLoad.java

/**
 * Attempt to read file with a given CSV format, and if successful, return
 * the number of rows in the file.//from  w  w  w  .  ja  va  2  s  .co  m
 * 
 * @param file to check for csv rows.
 * @param formatToTry the CSV format to try to read the file with.
 * @return number of rows in the file.
 * @throws IOException on a problem reading the header.
 * @throws FileNotFoundException on not finding the file.
 */
protected int readRows(File file, CSVFormat formatToTry) throws IOException, FileNotFoundException {
    int rows = 0;
    Reader reader = new FileReader(file);

    CSVParser csvParser = new CSVParser(reader, formatToTry);
    Iterator<CSVRecord> iterator = csvParser.iterator();
    while (iterator.hasNext()) {
        iterator.next();
        rows++;
    }
    csvParser.close();
    reader.close();
    return rows;
}