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

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

Introduction

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

Prototype

@Override
    public void close() throws IOException 

Source Link

Usage

From source file:org.gitia.jdataanalysis.JDataAnalysis.java

/**
 *
 * @param list/*from w w  w . j  a v a  2  s  .c o  m*/
 * @param folder
 * @param fileName
 */
public void save(List<String> list, String folder, String fileName) {
    String NEW_LINE_SEPARATOR = "\n";

    FileWriter fileWriter = null;

    CSVPrinter csvFilePrinter = null;

    //Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
    try {
        //initialize FileWriter object
        File file = new File(folder + "/" + fileName);
        fileWriter = new FileWriter(file);

        //initialize CSVPrinter object 
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

        //Create CSV file header
        //csvFilePrinter.printRecord(headers);
        //Write a new student object list to the CSV file
        for (int i = 0; i < list.size(); i++) {
            //List studentDataRecord = new ArrayList();
            csvFilePrinter.printRecord(list.get(i));
        }
        System.out.println("CSV file was created successfully !!!");
        System.out.println(folder + "/" + fileName);

    } catch (Exception e) {
        System.out.println("Error in CsvFileWriter !!!");
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();
        } catch (IOException e) {
            System.out.println("Error while flushing/closing fileWriter/csvPrinter !!!");
            e.printStackTrace();
        }
    }
}

From source file:org.kuali.test.runner.execution.TestExecutionContext.java

private void writePerformanceDataFile(File f) {
    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;

    //Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = CSVFormat.EXCEL.withRecordSeparator("\n");

    try {/*from  ww  w .ja va 2  s.c o m*/
        //initialize FileWriter object
        fileWriter = new FileWriter(f);

        //initialize CSVPrinter object
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

        //Create CSV file header
        csvFilePrinter.printRecord(Arrays.asList(PERFORMANCE_DATA_HEADER));

        //Write a new student object list to the CSV file
        for (String[] rec : performanceData) {
            csvFilePrinter.printRecord(Arrays.asList(rec));
        }
    }

    catch (Exception ex) {
        LOG.error(ex.toString(), ex);
    }

    finally {
        try {
            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
        }

        catch (Exception e) {
        }

        try {
            if (csvFilePrinter != null) {
                csvFilePrinter.close();
            }
        }

        catch (Exception e) {
        }
    }
}

From source file:org.languagetool.rules.spelling.suggestions.SuggestionChangesTest.java

public void testChanges() throws IOException, InterruptedException {

    File configFile = new File(System.getProperty("config", "SuggestionChangesTestConfig.json"));
    ObjectMapper mapper = new ObjectMapper(new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS));
    SuggestionChangesTestConfig config = mapper.readValue(configFile, SuggestionChangesTestConfig.class);

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String timestamp = dateFormat.format(new Date());
    Path loggingFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.log", timestamp));
    Path datasetFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.csv", timestamp));

    BufferedWriter writer = Files.newBufferedWriter(loggingFile);
    CSVPrinter datasetWriter = new CSVPrinter(Files.newBufferedWriter(datasetFile),
            CSVFormat.DEFAULT.withEscape('\\'));
    List<String> datasetHeader = new ArrayList<>(
            Arrays.asList("sentence", "correction", "covered", "replacement", "dataset_id"));

    SuggestionsChanges.init(config, writer);
    writer.write("Evaluation configuration: \n");
    String configContent = String.join("\n", Files.readAllLines(configFile.toPath()));
    writer.write(configContent);/*from w  w  w.  j ava2s.  c  o m*/
    writer.write("\nRunning experiments: \n");
    int experimentId = 0;
    for (SuggestionChangesExperiment experiment : SuggestionsChanges.getInstance().getExperiments()) {
        experimentId++;
        writer.write(String.format("#%d: %s%n", experimentId, experiment));
        datasetHeader.add(String.format("experiment_%d_suggestions", experimentId));
        datasetHeader.add(String.format("experiment_%d_metadata", experimentId));
        datasetHeader.add(String.format("experiment_%d_suggestions_metadata", experimentId));
    }
    writer.newLine();
    datasetWriter.printRecord(datasetHeader);

    BlockingQueue<SuggestionTestData> tasks = new LinkedBlockingQueue<>(1000);
    ConcurrentLinkedQueue<Pair<SuggestionTestResultData, String>> results = new ConcurrentLinkedQueue<>();
    List<SuggestionTestThread> threads = new ArrayList<>();
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
        SuggestionTestThread worker = new SuggestionTestThread(tasks, results);
        worker.start();
        threads.add(worker);
    }

    // Thread for writing results from worker threads into CSV
    Thread logger = new Thread(() -> {
        try {
            long messages = 0;
            //noinspection InfiniteLoopStatement
            while (true) {
                Pair<SuggestionTestResultData, String> message = results.poll();
                if (message != null) {
                    writer.write(message.getRight());

                    SuggestionTestResultData result = message.getLeft();
                    int datasetId = 1 + config.datasets.indexOf(result.getInput().getDataset());
                    if (result != null && result.getSuggestions() != null && !result.getSuggestions().isEmpty()
                            && result.getSuggestions().stream()
                                    .noneMatch(m -> m.getSuggestedReplacements() == null
                                            || m.getSuggestedReplacements().isEmpty())) {

                        List<Object> record = new ArrayList<>(Arrays.asList(result.getInput().getSentence(),
                                result.getInput().getCorrection(), result.getInput().getCovered(),
                                result.getInput().getReplacement(), datasetId));
                        for (RuleMatch match : result.getSuggestions()) {
                            List<String> suggestions = match.getSuggestedReplacements();
                            record.add(mapper.writeValueAsString(suggestions));
                            // features extracted by SuggestionsOrdererFeatureExtractor
                            record.add(mapper.writeValueAsString(match.getFeatures()));
                            List<SortedMap<String, Float>> suggestionsMetadata = new ArrayList<>();
                            for (SuggestedReplacement replacement : match.getSuggestedReplacementObjects()) {
                                suggestionsMetadata.add(replacement.getFeatures());
                            }
                            record.add(mapper.writeValueAsString(suggestionsMetadata));
                        }
                        datasetWriter.printRecord(record);
                    }

                    if (++messages % 1000 == 0) {
                        writer.flush();
                        System.out.printf("Evaluated %d corrections.%n", messages);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    logger.setDaemon(true);
    logger.start();

    // format straight from database dump
    String[] header = { "id", "sentence", "correction", "language", "rule_id", "suggestion_pos",
            "accept_language", "country", "region", "created_at", "updated_at", "covered", "replacement",
            "text_session_id", "client" };

    int datasetId = 0;
    // read data, send to worker threads via queue
    for (SuggestionChangesDataset dataset : config.datasets) {

        writer.write(String.format("Evaluating dataset #%d: %s.%n", ++datasetId, dataset));

        CSVFormat format = CSVFormat.DEFAULT;
        if (dataset.type.equals("dump")) {
            format = format.withEscape('\\').withNullString("\\N").withHeader(header);
        } else if (dataset.type.equals("artificial")) {
            format = format.withEscape('\\').withFirstRecordAsHeader();
        }
        try (CSVParser parser = new CSVParser(new FileReader(dataset.path), format)) {
            for (CSVRecord record : parser) {

                String lang = record.get("language");
                String rule = dataset.type.equals("dump") ? record.get("rule_id") : "";
                String covered = record.get("covered");
                String replacement = record.get("replacement");
                String sentence = record.get("sentence");
                String correction = record.isSet("correction") ? record.get("correction") : "";
                String acceptLanguage = dataset.type.equals("dump") ? record.get("accept_language") : "";

                if (sentence == null || sentence.trim().isEmpty()) {
                    continue;
                }

                if (!config.language.equals(lang)) {
                    continue; // TODO handle auto maybe?
                }
                if (dataset.type.equals("dump") && !config.rule.equals(rule)) {
                    continue;
                }

                // correction column missing in export from doccano; workaround
                if (dataset.enforceCorrect && !record.isSet("correction")) {
                    throw new IllegalStateException("enforceCorrect in dataset configuration enabled,"
                            + " but column 'correction' is not set for entry " + record);
                }

                if (dataset.type.equals("dump") && dataset.enforceAcceptLanguage) {
                    if (acceptLanguage != null) {
                        String[] entries = acceptLanguage.split(",", 2);
                        if (entries.length == 2) {
                            String userLanguage = entries[0]; // TODO: what to do with e.g. de-AT,de-DE;...
                            if (!config.language.equals(userLanguage)) {
                                continue;
                            }
                        }
                    }
                }

                tasks.put(new SuggestionTestData(lang, sentence, covered, replacement, correction, dataset));
            }
        }

    }

    for (Thread t : threads) {
        t.join();
    }
    logger.join(10000L);
    logger.interrupt();
    datasetWriter.close();
}

From source file:org.openo.client.cli.fw.output.print.OpenOCommandPrint.java

/**
 * Print output in csv format.//w  ww . j  av a2s .  com
 *
 * @return string
 * @throws OpenOCommandOutputPrintingFailed
 *             exception
 */
public String printCsv() throws OpenOCommandOutputPrintingFailed {
    StringWriter writer = new StringWriter();
    CSVPrinter printer = null;
    try {
        CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
        printer = new CSVPrinter(writer, formattor);

        List<List<Object>> rows = this.formRows(false);

        for (int i = 0; i < this.findMaxRows(); i++) {
            printer.printRecord(rows.get(i));
        }

        return writer.toString();
    } catch (IOException e) {
        throw new OpenOCommandOutputPrintingFailed(e);
    } finally {
        try {
            if (printer != null) {
                printer.close();
            }
            writer.close();
        } catch (IOException e) {
            throw new OpenOCommandOutputPrintingFailed(e); // NOSONAR
        }
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.scheduler.server.ExportUsageServlet.java

private String csvExport(String sessionId, String user, Date startDate, Date endDate)
        throws ServiceException, RestServerException, IOException {
    Object[] header = { "Owner", "Project", "Job Id", "Job Name", "Job Duration", "Task Id", "Task Name",
            "Task Node Number", "Task Start Time", "Task Finished Time", "Task Duration" };
    List<JobUsage> jobUsages = ((SchedulerServiceImpl) Service.get()).getUsage(sessionId, user, startDate,
            endDate);/*from   ww  w.java2  s.  c o m*/
    StringBuilder sb = new StringBuilder();
    CSVPrinter csvFilePrinter = null;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(LINE_SEPARATOR);
    csvFilePrinter = new CSVPrinter(sb, csvFileFormat);
    csvFilePrinter.printRecord(header);
    for (JobUsage jobUsage : jobUsages) {
        for (TaskUsage taskUsage : jobUsage.getTaskUsages()) {
            csvFilePrinter.printRecord(jobUsage.getOwner(), jobUsage.getProject(), jobUsage.getJobId(),
                    jobUsage.getJobName(), jobUsage.getJobDuration(), taskUsage.getTaskId(),
                    taskUsage.getTaskName(), taskUsage.getTaskNodeNumber(), taskUsage.getTaskStartTime(),
                    taskUsage.getTaskFinishedTime(), taskUsage.getTaskExecutionDuration());
        }
    }
    csvFilePrinter.close();
    return sb.toString();
}

From source file:org.qamatic.mintleaf.tools.CsvExportFlavour.java

@Override
public void export(ResultSet resultSet) throws MintleafException {
    CSVPrinter printer = null;
    try {//from   w w w  .j a v a  2 s  .  c om
        printer = new CSVPrinter(writer, CSVFormat.EXCEL.withHeader(resultSet));
        printer.printRecords(resultSet);
        printer.close();
    } catch (SQLException e) {
        throw new MintleafException(e);
    } catch (IOException e) {
        throw new MintleafException(e);

    }
}

From source file:org.seasr.meandre.components.transform.totext.TokenCountsToText.java

@Override
public void executeCallBack(ComponentContext cc) throws Exception {
    Map<String, Integer> tokenCounts = BasicDataTypesTools
            .IntegerMapToMap((IntegersMap) cc.getDataComponentFromInput(INPUT_TOKEN_COUNTS));

    console.finer("received token counts: " + tokenCounts.toString());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, false, encoding);

    CSVPrinter printer = new CSVPrinter(ps, format);
    for (Entry<String, Integer> token : tokenCounts.entrySet()) {
        printer.printRecord(token.getKey(), token.getValue());
    }/*from w  ww  .j a  va  2  s. c  om*/
    printer.close();

    cc.pushDataComponentToOutput(OUT_TEXT, BasicDataTypesTools.stringToStrings(baos.toString(encoding)));
}

From source file:org.seasr.meandre.components.transform.totext.TokenDoubleValuesToText.java

@Override
public void executeCallBack(ComponentContext cc) throws Exception {
    Map<String, Double> tokenValues = BasicDataTypesTools
            .DoubleMapToMap((DoublesMap) cc.getDataComponentFromInput(INPUT_TOKEN_VALUES));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, false, encoding);

    CSVPrinter printer = new CSVPrinter(ps, format);
    for (Entry<String, Double> token : tokenValues.entrySet()) {
        printer.printRecord(token.getKey(), token.getValue());
    }//from   w ww  .  ja va2 s.com
    printer.close();

    //      printDoublesMap(ps, tokenValues, this.iCount, this.iOffset);
    //      System.out.println(String.format("TokenDoubleValuesToText: printed %d values making %d chars of output",tokenValues.getKeyCount()-this.iOffset,baos.size()));
    console.fine(String.format("printed %d values making %d chars of output", tokenValues.size() - this.iOffset,
            baos.size()));

    cc.pushDataComponentToOutput(OUT_TEXT, BasicDataTypesTools.stringToStrings(baos.toString(encoding)));
}

From source file:org.shareok.data.documentProcessor.CsvHandler.java

/**
 * Output the data into a new CSV file//ww w.j  a  v a  2  s. c  om
 * <p>Uses "\n" as the line separator</P>
 * 
 * @param newFileName : String. 
 * If the newFileName is not specified, a "-copy" will be attached to the old file name for the new CSV file
 * 
 * @return the path of the new CSV file
 */
public String outputData(String newFileName) {
    if (null == data) {
        readData();
    }

    if (null != data) {
        FileWriter fileWriter = null;
        CSVPrinter csvFilePrinter = null;
        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
        try {
            if (null == newFileName || newFileName.equals("")) {
                newFileName = fileName.substring(0, fileName.indexOf(".csv")) + "-copy.csv";
            }
            fileWriter = new FileWriter(newFileName);

            csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
            csvFilePrinter.printRecord(Arrays.asList(fileHeadMapping));
            Map<String, String[]> records = new HashMap<>();
            List<String> headingList = Arrays.asList(fileHeadMapping);

            Iterator it = data.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                String value = (String) data.get(key);
                String[] keyInfo = key.split("-");
                String row = keyInfo[keyInfo.length - 1];
                String column = key.replace("-" + row, "");

                if (null == records.get(row)) {
                    String[] dataRecord = new String[fileHeadMapping.length];
                    dataRecord[headingList.indexOf(column)] = value;
                    records.put(row, dataRecord);
                } else {
                    String[] dataRecord = records.get(row);
                    dataRecord[headingList.indexOf(column)] = value;
                }
            }

            Iterator it2 = records.keySet().iterator();
            while (it2.hasNext()) {
                String key = (String) it2.next();
                String[] value = (String[]) records.get(key);
                csvFilePrinter.printRecord(Arrays.asList(value));
            }
        } catch (Exception e) {
            System.out.println("Error in CsvFileWriter!\n");
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
                csvFilePrinter.close();
            } catch (IOException e) {
                System.out.println("Error while flushing/closing fileWriter/csvPrinter\n");
                e.printStackTrace();
            }
        }
    }
    return newFileName;
}

From source file:org.thegalactic.context.io.ContextSerializerCsv.java

/**
 * Write a context to a csv file./*from  ww  w  . ja v a2  s.co m*/
 *
 * The following format is respected:
 *
 * The first line contains the attribute names, the other lines contains the
 * observations identifier followed by boolean values
 *
 * ~~~
 * "",a,b,c,d,e
 * 1,1,0,1,0,0
 * 2,1,1,0,0,0
 * 3,0,1,0,1,1
 * 4,0,0,1,0,1
 * ~~~
 *
 * @param context a context to write
 * @param file    a file
 *
 * @throws IOException When an IOException occurs
 */
public void write(Context context, BufferedWriter file) throws IOException {
    CSVPrinter printer = new CSVPrinter(file, CSVFormat.RFC4180);

    // Get the observations and the attributes
    TreeSet<Comparable> observations = context.getObservations();
    TreeSet<Comparable> attributes = context.getAttributes();

    // Prepare the attribute line
    printer.print("");

    for (Comparable attribute : attributes) {
        // Write each attribute
        printer.print(attribute);
    }

    printer.println();

    for (Comparable observation : observations) {
        // Write the observation
        printer.print(observation);

        // Write the extent/intents
        for (Comparable attribute : attributes) {
            if (context.getIntent(observation).contains(attribute)) {
                printer.print(1);
            } else {
                printer.print(0);
            }
        }

        printer.println();
    }

    printer.close();
}