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

The following examples show how to use au.com.bytecode.opencsv.CSVWriter#writeAll() . 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: OpenCSVParserExample.java    From journaldev with MIT License 5 votes vote down vote up
private static void writeCSVData(List<Employee> emps) throws IOException {
	StringWriter writer = new StringWriter();
	CSVWriter csvWriter = new CSVWriter(writer,'#');
	List<String[]> data  = toStringArray(emps);
	csvWriter.writeAll(data);
	csvWriter.close();
	System.out.println(writer);
}
 
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: ResultSetController.java    From ZenQuery with Apache License 2.0 5 votes vote down vote up
private String getCsvResults(List<Map<String, Object>> rows) {
    List<String[]> outputRows = new ArrayList<String[]>();
    Boolean first = true;

    StringWriter stringWriter = new StringWriter();
    CSVWriter csvWriter = new CSVWriter(stringWriter);

    for (Map<String, Object> row : rows) {
        if (first) {
            Set<String> keys = row.keySet();
            String[] columnTitles = new String[keys.size()];
            columnTitles = keys.toArray(columnTitles);
            outputRows.add(columnTitles);
            first = false;
        }

        Collection<Object> values = row.values();
        Object[] columnValues = new Object[values.size()];
        columnValues = values.toArray(columnValues);

        Integer numberOfValues = values.size();
        String[] columnOutputValues = new String[numberOfValues];
        for (int i = 0; i < numberOfValues; i++) {
            Object columnValue = columnValues[i];
            if (columnValue != null) {
              columnOutputValues[i] = columnValue.toString();
            } else {
              columnOutputValues[i] = "";
            }
        }
        outputRows.add(columnOutputValues);
    }

    csvWriter.writeAll(outputRows);

    return stringWriter.toString();
}
 
Example 4
Source File: ContainerUtils.java    From sofa-acts with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param clsChild
 * @param cvsChild
 * @param csvParent
 * @throws FileNotFoundException
 * @throws IOException
 */
private static void csvReplaceObj(Class<?> clsChild, String cvsChild, String csvParent,
                                  Set<String> sbfWarn) {
    try {
        if (null != clsChild) {
            FileReader fReader = new FileReader(csvParent);
            CSVReader csvReader = new CSVReader(fReader);
            List<String[]> readLine = csvReader.readAll();
            List<String> addLine = new ArrayList<String>(6);
            int i = 0;
            for (String[] readDetail : readLine) {
                if (StringUtils.equals(readDetail[2], "java.lang.Object")) {
                    addLine.add(readDetail[0]);
                    addLine.add(readDetail[1]);
                    addLine.add(clsChild.getName());
                    addLine.add(readDetail[3]);

                    if (CSVApisUtil.isWrapClass(clsChild)) {
                        if (StringUtils.equals(clsChild.getName(), "java.lang.String")) {
                            addLine.add("Y");
                            addLine.add("1");
                        } else {
                            CSVApisUtil.addSimpleValue("", clsChild, addLine);
                        }
                    } else if (StringUtils.isNotBlank(cvsChild)) {
                        File openFile = new File(cvsChild);
                        if (openFile.exists()) {
                            addLine.add("Y");
                            addLine.add(CSVApisUtil.cutCsvName(cvsChild) + "@1");
                        } else {
                            addLine.add("N");
                            addLine.add("");
                        }
                    } else {
                        addLine.add("N");
                        addLine.add("");
                    }
                    break;
                }
                i++;
            }

            readLine.set(i, addLine.toArray(new String[addLine.size()]));

            csvReader.close();
            fReader.close();

            FileWriter fWrite = new FileWriter(csvParent);
            CSVWriter csvWriter = new CSVWriter(fWrite);
            csvWriter.writeAll(readLine);
            csvWriter.close();
            fWrite.close();
        }
    } catch (Exception e) {
        sbfWarn.add("failed to add sub-file [" + CSVApisUtil.cutCsvName(cvsChild)
                    + "] to file [" + CSVApisUtil.cutCsvName(csvParent) + "]");
    }
}
 
Example 5
Source File: LicenceController.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
private String buildCsv(List<Licence> licences) throws IOException {
	List<String[]> csvLines = new ArrayList<String[]>();
	String[] csvHeader = { "RightsholderName", "Owner", "OwnerEmailAddress", "TransactionDate", "Title", "Subtitle", "Edition", "Author", "Identifier", "SubjectArea", "OutletName", "OutletId", "OutletCity", "OutletCountry", "OutletCurrency", "OutletServiceCharge", "NumberOfCopies", "Format", "DoubleSidedSheets", "CreditsReserved", "DownloadedYN", "CreditsCharged", "DollarEquivalent", "PaperightFee", "AmountOwingToRightsholder" };
	csvLines.add(csvHeader);
	for (Licence licence : licences) {
		Product product = licence.getProduct();
		Address address = licence.getCompany().getAddressContextByType(AddressContextType.DEFAULT_PRIMARY).getAddress();
		Company owner = licence.getOwnerCompany();
		String ownerEmail = null;
		String ownerName = null;
		if (owner != null) {
		    ownerName = owner.getName();
		    ownerEmail = owner.getEmail();
		}
		String[] csvLine = new String[csvHeader.length];
		csvLine[0] = product.getPublisher();//RightsholderName
		if (!StringUtils.isBlank(ownerName)) {
		    csvLine[1] = ownerName; //Owner
		} else {
		    csvLine[1] = ""; //Owner
		}
		if (!StringUtils.isBlank(ownerEmail)) {
		    csvLine[2] = ownerEmail;//Owner email address
		} else {
		    csvLine[2] = "";//Owner email address
		}
		csvLine[3] = licence.getCreatedDate().toString();//TransactionDate
		csvLine[4] = product.getTitle();//Title
		csvLine[5] = product.getSubTitle();//Subtitle
		csvLine[6] = product.getEdition();//Edition
		csvLine[7] = product.getPrimaryCreators();//Author
		csvLine[8] = product.getIdentifier();//Identifier
		csvLine[9] = product.getSubjectArea();//SubjectArea
		csvLine[10] = licence.getCompany().getName();//OutletName
		csvLine[11] = licence.getCompany().getId().toString();//OutletId
		csvLine[12] = address.getAddressLine4();//OutletCity
		csvLine[13] = address.getCountry().getName();//OutletCountry
		csvLine[14] = licence.getCurrencyCode();//OutletCurrency
		csvLine[15] = licence.getOutletCharge().toString();//OutletServiceCharge
		csvLine[16] = licence.getNumberOfCopies() + "";//NumberOfCopies
		csvLine[17] = licence.getPageLayout().toString();//Format
		csvLine[18] = licence.getPageExtent() + "";//DoubleSidedSheets
		csvLine[19] = licence.getCostInCredits().toString();//CreditsReserved
		csvLine[20] = licence.isDownloaded() ? "Y" : "N" ;//DownloadedYN
		csvLine[21] = licence.isDownloaded() ? licence.getCostInCredits().toString() : "";//CreditsCharged
		BigDecimal dollarEquivalent = licence.getCostInCredits().multiply(licence.getPaperightCreditToBaseCurrencyRate()).setScale(2, RoundingMode.UP);
		BigDecimal paperightFree = dollarEquivalent.multiply(BigDecimal.valueOf(0.2)).setScale(2, RoundingMode.HALF_UP);
		csvLine[22] = dollarEquivalent.toString();//DollarEquivalent
		csvLine[23] = paperightFree.toString();//PaperightFee
		csvLine[24] = dollarEquivalent.subtract(paperightFree).toString();//AmountOwingToRightsholder
		csvLines.add(csvLine);
	}
	Writer writer = new StringWriter();
	CSVWriter csvWriter = new CSVWriter(writer);
	try {
		csvWriter.writeAll(csvLines);
		return writer.toString();
	} finally {
		csvWriter.close();
	} 
}
 
Example 6
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);
    }
}