Java Code Examples for au.com.bytecode.opencsv.CSVWriter#flush()

The following examples show how to use au.com.bytecode.opencsv.CSVWriter#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CsvUtils.java    From pxf with Apache License 2.0 6 votes vote down vote up
/**
 * Write {@link Table} to a CSV file.
 *
 * @param table {@link Table} contains required data list to write to CSV file
 * @param targetCsvFile to write the data Table
 * @throws IOException
 */
public static void writeTableToCsvFile(Table table, String targetCsvFile)
		throws IOException {

	// create CsvWriter using FileWriter
	CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(targetCsvFile)));

	try {
		// go over list and write each inner list to csv file
		for (List<String> currentList : table.getData()) {

			Object[] objectArray = currentList.toArray();

			csvWriter.writeNext(Arrays.copyOf(currentList.toArray(), objectArray.length, String[].class));
		}
	} finally {

		// flush and close writer
		csvWriter.flush();
		csvWriter.close();
	}
}
 
Example 2
Source File: DecomposeSingularValues.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeMatrix(final RealMatrix m, final File outputFilename) throws IOException {
    final List<String []> textTable = new ArrayList<>();
    for (int i = 0; i < m.getRowDimension(); i ++){
        textTable.add(Arrays.stream(m.getRow(i)).mapToObj(Double::toString).toArray(String[]::new));
    }
    final FileWriter fw = new FileWriter(outputFilename);
    CSVWriter csvWriter = new CSVWriter(fw, '\t', CSVWriter.NO_QUOTE_CHARACTER);
    csvWriter.writeAll(textTable);
    csvWriter.flush();
    csvWriter.close();
}
 
Example 3
Source File: DataSetExportServicesImpl.java    From dashbuilder with Apache License 2.0 4 votes vote down vote up
public org.uberfire.backend.vfs.Path exportDataSetCSV(DataSet dataSet) {
    try {
        if (dataSet == null) {
            throw new IllegalArgumentException("Null dataSet specified!");
        }
        int columnCount = dataSet.getColumns().size();
        int rowCount = dataSet.getRowCount();

        List<String[]> lines = new ArrayList<>(rowCount+1);

        String[] line = new String[columnCount];
        for (int cc = 0; cc < columnCount; cc++) {
            DataColumn dc = dataSet.getColumnByIndex(cc);
            line[cc] = dc.getId();
        }
        lines.add(line);

        for (int rc = 0; rc < rowCount; rc++) {
            line = new String[columnCount];
            for (int cc = 0; cc < columnCount; cc++) {
                line[cc] = formatAsString(dataSet.getValueAt(rc, cc));
            }
            lines.add(line);
        }

        String tempCsvFile = uuidGenerator.newUuid() + ".csv";
        Path tempCsvPath = gitStorage.createTempFile(tempCsvFile);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(tempCsvPath)));
        CSVWriter writer = new CSVWriter(bw,
                DEFAULT_SEPARATOR_CHAR.charAt(0),
                DEFAULT_QUOTE_CHAR.charAt(0),
                DEFAULT_ESCAPE_CHAR.charAt(0));
        writer.writeAll(lines);
        writer.close();
        writer.flush();
        writer.close();
        return Paths.convert(tempCsvPath);
    }
    catch (Exception e) {
        throw exceptionManager.handleException(e);
    }
}
 
Example 4
Source File: OrganizationExport.java    From usergrid with Apache License 2.0 2 votes vote down vote up
@Override
public void runTool( CommandLine line ) throws Exception {
    startSpring();

    setVerbose( line );

    prepareBaseOutputFileName( line );

    outputDir = createOutputParentDir();

    String queryString = line.getOptionValue( QUERY_ARG );

    Query query = Query.fromQL( queryString );

    logger.info( "Export directory: {}", outputDir.getAbsolutePath() );

    CSVWriter writer = new CSVWriter( new FileWriter( outputDir.getAbsolutePath() + "/admins.csv" ), ',' );

    writer.writeNext( new String[] { "Org uuid", "Org Name", "Admin uuid", "Admin Name", "Admin Email", "Admin Created Date" } );

    Results organizations = null;

    do {

        organizations = getOrganizations( query );

        for ( Entity organization : organizations.getEntities() ) {
            final String orgName = organization.getProperty( "path" ).toString();
            final UUID orgId = organization.getUuid();

            logger.info( "Org Name: {} key: {}", orgName, orgId );

            for ( UserInfo user : managementService.getAdminUsersForOrganization( organization.getUuid() ) ) {

                Entity admin = managementService.getAdminUserEntityByUuid( user.getUuid() );

                Long createdDate = ( Long ) admin.getProperties().get( "created" );

                writer.writeNext( new String[] { orgId.toString(),
                        orgName, user.getUuid().toString(), user.getName(), user.getEmail(),
                        createdDate == null ? "Unknown" : sdf.format( new Date( createdDate ) )
                } );
            }
        }

        query.setCursor( organizations.getCursor() );
    }
    while ( organizations != null && organizations.hasCursor() );

    logger.info( "Completed export" );

    writer.flush();
    writer.close();
}