Example usage for org.apache.commons.csv CSVFormat DEFAULT

List of usage examples for org.apache.commons.csv CSVFormat DEFAULT

Introduction

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

Prototype

CSVFormat DEFAULT

To view the source code for org.apache.commons.csv CSVFormat DEFAULT.

Click Source Link

Document

Standard comma separated format, as for #RFC4180 but allowing empty lines.

Usage

From source file:com.streamsets.pipeline.lib.generator.delimited.DelimitedDataGeneratorFactory.java

@Override
public DataGenerator getGenerator(OutputStream os) throws IOException {
    CSVFormat csvFormat = getSettings().getMode(CsvMode.class).getFormat();
    if (getSettings().getMode(CsvMode.class) == CsvMode.CUSTOM) {
        csvFormat = CSVFormat.DEFAULT
                .withDelimiter((char) getSettings().getConfig(DelimitedDataConstants.DELIMITER_CONFIG))
                .withEscape((char) getSettings().getConfig(DelimitedDataConstants.ESCAPE_CONFIG))
                .withQuote((char) getSettings().getConfig(DelimitedDataConstants.QUOTE_CONFIG));
    }/*  w w w  . j  a v  a2 s. c  o m*/
    return new DelimitedCharDataGenerator(createWriter(os), csvFormat, header, headerKey, valueKey,
            replaceNewLines ? replaceNewLinesString : null);
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVFileSchemaParser.java

private CSVFormat createCSVFormat(String sampleData) throws IOException {
    CSVFormat format;//  w w  w  . ja  v  a2  s  .co m
    if (autoDetect) {
        CSVAutoDetect autoDetect = new CSVAutoDetect();
        format = autoDetect.detectCSVFormat(sampleData, this.headerRow, this.separatorChar);
        this.separatorChar = Character.toString(format.getDelimiter());
        this.quoteChar = Character.toString(format.getQuoteCharacter());
    } else {
        format = CSVFormat.DEFAULT.withAllowMissingColumnNames();

        if (StringUtils.isNotEmpty(separatorChar)) {
            format = format.withDelimiter(toChar(separatorChar).charAt(0));
        }
        if (StringUtils.isNotEmpty(escapeChar)) {
            format = format.withEscape(toChar(escapeChar).charAt(0));
        }
        if (StringUtils.isNotEmpty(quoteChar)) {
            format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(toChar(quoteChar).charAt(0));
        }
    }

    return format;
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemResponseSimulator.java

/**
 * Generates a comma separated file (CSV file) of item responses.
 *
 * @param outputFile complete path and file name of output file
 * @param includeID include an examinee ID number in the first column if true. Omits the ID if false.
 * @param includeHeader if true will include variable names in first row of CSV file.
 * @throws IOException//from   w  w w  .  jav a 2  s . c o m
 */
public void generateData(String outputFile, boolean includeID, boolean includeHeader) throws IOException {
    byte[][] x = generateData();
    int baseID = nPeople * 10 + 1;

    Writer writer = null;
    CSVPrinter printer = null;
    File file = new File(outputFile);

    try {
        writer = new OutputStreamWriter(new FileOutputStream(file));
        printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#'));

        if (includeHeader) {
            if (includeID)
                printer.print("ID");
            for (int j = 0; j < nItems; j++) {
                printer.print("V" + (j + 1));
            }
            printer.println();
        }

        for (int i = 0; i < nPeople; i++) {
            if (includeID)
                printer.print(baseID);
            for (int j = 0; j < nItems; j++) {
                printer.print(x[i][j]);
            }
            printer.println();
            baseID++;
        }
    } catch (IOException ex) {
        throw (ex);
    } finally {
        if (writer != null)
            writer.close();
        if (printer != null)
            printer.close();
    }

}

From source file:com.itemanalysis.jmetrik.file.JmetrikOutputWriter.java

private void saveCsvFile(File outputFile, Outputter outputter) throws IOException {

    ArrayList<VariableAttributes> variables = outputter.getColumnAttributes();
    LinkedHashMap<VariableName, VariableAttributes> variableAttributeMap = new LinkedHashMap<VariableName, VariableAttributes>();
    String[] header = new String[variables.size()];
    int hIndex = 0;
    for (VariableAttributes v : variables) {
        variableAttributeMap.put(v.getName(), v);
        header[hIndex] = v.getName().toString();
        hIndex++;/*w w w . j  av a 2 s  .  c  o m*/
    }

    Writer writer = null;
    CSVPrinter printer = null;

    try {
        //Ensure that file is a csv file.
        String fname = FilenameUtils.removeExtension(outputFile.getAbsolutePath());
        outputFile = new File(fname + ".csv");

        writer = new OutputStreamWriter(new FileOutputStream(outputFile));
        printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#').withHeader(header));

        Iterator<Object[][]> iter = outputter.iterator();
        Object[][] outputChunk = null;

        while (iter.hasNext()) {
            outputChunk = iter.next();

            for (int i = 0; i < outputChunk.length; i++) {
                printer.printRecord(outputChunk[i]);
            }

        }

    } catch (IOException ex) {
        throw ex;
    } finally {
        printer.close();
    }

}

From source file:biz.ganttproject.impex.csv.GanttCSVExport.java

private CSVFormat getCSVFormat() {
    CSVFormat format = CSVFormat.DEFAULT.withEscape('\\');
    if (myCsvOptions.sSeparatedChar.length() == 1) {
        format = format.withDelimiter(myCsvOptions.sSeparatedChar.charAt(0));
    }/*from  ww w.j a  va  2s . c  o m*/
    if (myCsvOptions.sSeparatedTextChar.length() == 1) {
        format = format.withQuote(myCsvOptions.sSeparatedTextChar.charAt(0));
    }

    return format;
}

From source file:com.goeuro.goeurotest.service.Services.java

/**
 * Write CSV file using list of records and pre defined static header
 *
 * @param recordsList//from   w ww  .  j  av  a 2 s.  co m
 * @throws Exception
 */
public void writeCSV(List recordsList) throws Exception {
    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;
    try {
        CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(Defines.NEW_LINE_SEPARATOR);
        fileWriter = new FileWriter(Defines.FILE_NAME);
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
        csvFilePrinter.printRecord(Defines.FILE_HEADER);
        for (Object recordList : recordsList) {
            csvFilePrinter.printRecords(recordList);
        }
        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();
    } catch (IOException ex) {
        throw new Exception("IOException occured while writing CSV file " + ex.getMessage());
    }
}

From source file:javalibs.CSVDataNormalizer.java

public void normalize() {
    BufferedWriter bw = null;/* w  w  w.  j  ava2s. co  m*/
    CSVPrinter printer = null;

    try {
        bw = Files.newBufferedWriter(Paths.get(this.savePath));
        printer = new CSVPrinter(bw, CSVFormat.DEFAULT.withHeader(this.headersInOrder));
    } catch (IOException e) {
        log_.die(e);
    }

    for (CSVRecord rec : this.allRecords) {
        List<String> writerCells = new ArrayList<>();
        for (int i = 0; i < this.numCols; ++i) {
            String colName = this.colNumToName.get(i);
            if (columnsToNormalize.contains(colName)) {
                double curVal = NumUtils.getDoubleFromStr(rec.get(colName));
                Pair<Double, Double> maxMin = this.colsToMaxMinPairs.get(colName);
                double normal = NumUtils.normalizeBetweenZeroOne(maxMin.right(), maxMin.left(), curVal);
                if (normal > 1.0) {
                    log_.warn("Normalized value greater than 1.0: " + normal + " from curVal: " + curVal
                            + " setting normal to 1.");
                    normal = 1.0;
                } else if (normal < 0.0) {
                    log_.warn("Normalized value less than 0.0: " + normal + " from curVal : " + curVal
                            + " setting normal to 0.");
                    normal = 0.0;
                }

                writerCells.add(Double.toString(normal));
            } else
                writerCells.add(rec.get(i));
        }
        try {
            printer.printRecord(writerCells.toArray());
        } catch (IOException e) {
            log_.die(e);
        }
    }
    try {
        printer.flush();
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java

private Character guessQuote(List<LineStats> lineStats) {
    Character[] quoteTypeSupported = { Character.valueOf('"'), Character.valueOf('\'') };
    boolean match = false;
    for (Character quoteType : quoteTypeSupported) {
        boolean quoteTypeFound = lineStats.stream()
                .anyMatch(lineStat -> lineStat.containsNoDelimCharOfType(quoteType));
        if (quoteTypeFound) {
            match = lineStats.stream().allMatch(lineStat -> lineStat.hasLegalQuotedStringOfChar(quoteType));
        }/*ww w  .  j av  a 2 s .  c o m*/
        if (match) {
            return quoteType;
        }
    }
    return CSVFormat.DEFAULT.getQuoteCharacter();
}

From source file:com.adobe.acs.commons.exporters.impl.users.UserExportServletTest.java

@Test
public void testWithGroupIndirectFilter() throws Exception {

    // Build parameters
    JsonObject params = buildParameterObject("indirect", "allusers");

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("params", params);

    context.request().setParameterMap(parameters);
    servlet.doGet(context.request(), context.response());

    assertEquals(context.response().getStatus(), 200);
    String output = context.response().getOutputAsString();

    CSVParser parser = CSVParser.parse(output, CSVFormat.DEFAULT.withHeader());
    assertAllUsersPresent(parser.getRecords(), "alice", "bob");
}

From source file:io.ecarf.core.cloud.task.processor.reason.phase2.ReasonUtils.java

/**
 * //w ww . j  a v  a2 s  .  c  o  m
 * @param file
 * @param writer
 * @param compressed
 * @return
 * @throws IOException 
 */
public static int reason(String inFile, String outFile, boolean compressed, Map<Long, Set<Triple>> schemaTerms,
        Set<Long> productiveTerms, DuplicatesBuster duplicatesBuster) throws IOException {

    log.info("Reasoning for file: " + inFile + ", memory usage: " + Utils.getMemoryUsageInGB() + "GB");

    int inferredTriples = 0;

    // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set
    try (BufferedReader reader = getQueryResultsReader(inFile, compressed);
            PrintWriter writer = new PrintWriter(new BufferedOutputStream(
                    new GZIPOutputStream(new FileOutputStream(outFile), Constants.GZIP_BUF_SIZE),
                    Constants.GZIP_BUF_SIZE));) {

        Iterable<CSVRecord> records;

        if (compressed) {
            // ignore first row subject,predicate,object
            records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(reader);

        } else {
            records = CSVFormat.DEFAULT.parse(reader);
        }

        Long term;

        for (CSVRecord record : records) {

            ETriple instanceTriple = ETriple.fromCSV(record.values());

            // TODO review for OWL ruleset
            if (SchemaURIType.RDF_TYPE.id == instanceTriple.getPredicate()) {

                term = instanceTriple.getObject(); // object

            } else {

                term = instanceTriple.getPredicate(); // predicate
            }

            Set<Triple> schemaTriples = schemaTerms.get(term);

            if ((schemaTriples != null) && !schemaTriples.isEmpty()) {
                productiveTerms.add(term);

                for (Triple schemaTriple : schemaTriples) {
                    Rule rule = GenericRule.getRule(schemaTriple);
                    Triple inferredTriple = rule.head(schemaTriple, instanceTriple);

                    if ((inferredTriple != null) && !duplicatesBuster.isDuplicate(inferredTriple)) {
                        writer.println(inferredTriple.toCsv());
                        inferredTriples++;
                    }
                }
            }

        }

    }

    return inferredTriples;
}