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

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

Introduction

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

Prototype

public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException 

Source Link

Document

Creates a printer that will print values to the given stream following the CSVFormat.

Usage

From source file:trainer.userinput.TrainingFileDB.java

public static void writeTestCsvFile(List<String> mlInputs, boolean append) {

    if (mlInputs.size() == 0)
        return;/*from w ww .ja v  a  2s  . c o  m*/

    // Create new students objects
    List<UserInputTrainingRecord> trainings = new ArrayList<UserInputTrainingRecord>();
    for (int index = 0; index < mlInputs.size(); index++) {
        trainings.add(new UserInputTrainingRecord(" ", mlInputs.get(index)));
    }

    FileWriter fileWriter = null;

    CSVPrinter csvFilePrinter = null;

    // Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = getCSVFormat();
    try {

        // initialize FileWriter object
        fileWriter = new FileWriter(UserInputsTrainer.TESTFILE, append);

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

        // Write a new student object list to the CSV file
        for (UserInputTrainingRecord student : trainings) {
            List<String> studentDataRecord = new ArrayList<String>();
            studentDataRecord.add(student.getInputLabelName());
            studentDataRecord.add(student.getInputMLHtmlCode());
            csvFilePrinter.printRecord(studentDataRecord);
        }

        System.out.println("CSV file was created successfully !!!");

    } catch (Exception e) {
        System.out.println("Error in CsvFileWriter !!!");
        e.printStackTrace();
    } 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:trainer.userinput.TrainingFileDB.java

public static void writeCacheCsvFile(String absoluteCacheFilePath, String beginningComments,
        String endingComments, List<UserInputTrainingRecord> trainedRecords, boolean append) {
    // Create new students objects

    FileWriter fileWriter = null;

    CSVPrinter csvFilePrinter = null;/* w  w w . j ava 2s  .c o  m*/

    // Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = getCSVFormat();
    try {
        if (trainedRecords.size() == 0) {
            fileWriter = new FileWriter(absoluteCacheFilePath, append);

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

            // Write a new student object list to the CSV file
            csvFilePrinter.printComment(beginningComments);
            csvFilePrinter.printComment(endingComments);

            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();
            return;
        }

        // initialize FileWriter object
        fileWriter = new FileWriter(absoluteCacheFilePath, append);

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

        // Write a new student object list to the CSV file
        csvFilePrinter.printComment(beginningComments);
        for (UserInputTrainingRecord student : trainedRecords) {
            List<String> studentDataRecord = new ArrayList<String>();
            studentDataRecord.add(student.getInputLabelName());
            studentDataRecord.add(student.getInputMLHtmlCode());

            csvFilePrinter.printRecord(studentDataRecord);
        }
        csvFilePrinter.printComment(endingComments);
        System.out.println("CSV file was created successfully !!!");

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