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.zanata.client.commands.stats.CsvStatisticsOutput.java

@Override
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
public void write(ContainerTranslationStatistics statistics) {
    try {/* w w w  . j a v a  2 s. c om*/
        OutputStreamWriter streamWriter = new OutputStreamWriter(System.out);
        try {
            CSVPrinter csvPrinter = new CSVPrinter(streamWriter,
                    CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR));

            try {
                writeToCsv(statistics, csvPrinter);
                csvPrinter.flush();
            } finally {
                csvPrinter.close();
            }
        } finally {
            streamWriter.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:password.pwm.svc.report.ReportCsvUtility.java

public void outputSummaryToCsv(final OutputStream outputStream, final Locale locale) throws IOException {
    final List<ReportSummaryData.PresentationRow> outputList = reportService.getSummaryData()
            .asPresentableCollection(pwmApplication.getConfig(), locale);
    final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(outputStream);

    for (final ReportSummaryData.PresentationRow presentationRow : outputList) {
        final List<String> headerRow = new ArrayList<>();
        headerRow.add(presentationRow.getLabel());
        headerRow.add(presentationRow.getCount());
        headerRow.add(presentationRow.getPct());
        csvPrinter.printRecord(headerRow);
    }//from w ww .jav  a2 s  .  co m

    csvPrinter.close();
}

From source file:password.pwm.svc.report.ReportService.java

public void outputSummaryToCsv(final OutputStream outputStream, final Locale locale) throws IOException {
    final List<ReportSummaryData.PresentationRow> outputList = summaryData
            .asPresentableCollection(pwmApplication.getConfig(), locale);
    final CSVPrinter csvPrinter = Helper.makeCsvPrinter(outputStream);

    for (final ReportSummaryData.PresentationRow presentationRow : outputList) {
        final List<String> headerRow = new ArrayList<>();
        headerRow.add(presentationRow.getLabel());
        headerRow.add(presentationRow.getCount());
        headerRow.add(presentationRow.getPct());
        csvPrinter.printRecord(headerRow);
    }//from   w ww . ja va 2s .  c o  m

    csvPrinter.close();
}

From source file:persistencia.ArchivoCSV.java

public void create() throws IOException {
    CSVPrinter csvFilePrinter = null;

    try {/*from   ww w  .  j  a  v a  2 s .  c o  m*/

        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

        csvFilePrinter = new CSVPrinter(file, csvFileFormat);

        csvFilePrinter.printRecord(FILE_HEADER);

        csvFilePrinter.printRecord(csv);

        System.out.println("CSV CORRECTAMENTE ESCRITO");

    } catch (Exception e) {
        System.out.println("Error en escritura");
    } finally {
        try {

            cerrarArchivo();
            csvFilePrinter.close();

        } catch (Exception e) {

            System.out.println("Error al cerrar archivo");
        }
    }

}

From source file:resources.TitleTag.java

public static boolean writeAsCsvTitleTagList(String fileName, List<TitleTag> titleTagList) {

    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;

    //        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(DELIMITER);
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withDelimiter('\t');

    boolean err = false;
    try {//from   www  .j ava 2  s  .  c  o  m
        fileWriter = new FileWriter(fileName);// Open File with fileWriter
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);// get Printer
        csvFilePrinter.printRecord(FILE_HEADER); // Write Header 

        for (TitleTag tag : titleTagList) {
            List tagRecord = new ArrayList();
            tagRecord.add(tag.getDbID());
            tagRecord.add(tag.getTitle());
            tagRecord.add(tag.getTag());
            csvFilePrinter.printRecord(tagRecord);
        }
        System.out.println("CSV File Recorded Successfylly");
    } catch (Exception ex) {
        err = true;
        System.out.println("Error in CSV File Writer ! Writing TItle - Tag table");
        ex.printStackTrace();
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();
        } catch (IOException e) {
            err = true;
            System.out.println("Error while flushing/closing fileWriter /csvPrinter !!");
            e.printStackTrace();
        } finally {
            return err;
        }
    }

}

From source file:trainer.userinput.TrainingFileDB.java

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

    if (mlInputs.size() == 0)
        return;//w  w  w  . j  av a  2  s.co  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;

    // Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = getCSVFormat();
    try {/*from www.  j  a  v  a 2s. co  m*/
        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();
        }
    }
}