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:de.britter.beyondstringutils.CsvExample.java

public static void main(String[] args) throws Exception {
    InputStream is = CsvExample.class.getResourceAsStream("heros.csv");
    Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader("First Name", "Last Name").withSkipHeaderRecord()
            .parse(new InputStreamReader(is));

    for (CSVRecord record : records) {
        String firstName = record.get("First Name");
        String lastName = record.get("Last Name");
        System.out.println(String.format("First Name: %s; Last Name: %s", firstName, lastName));
    }//ww  w.  j  a v  a2  s . c  om

}

From source file:com.blockhaus2000.csvviewer.CsvViewerMain.java

public static void main(final String[] args) {
    try (final CSVParser parser = new CSVParser(new InputStreamReader(System.in), CSVFormat.DEFAULT)) {
        final Table table = new Table();

        final Map<String, Integer> headerMap = parser.getHeaderMap();
        if (headerMap != null) {
            final TableRow headerRow = new TableRow();
            headerMap.keySet().forEach(headerData -> headerRow.addCell(new TableRowCell(headerData)));
            table.setHeaderRow(headerRow);
        }/*  w  w w  .j av a 2 s  .c  om*/

        final AtomicBoolean asHeader = new AtomicBoolean(headerMap == null);
        parser.getRecords().forEach(record -> {
            final TableRow row = new TableRow();
            record.forEach(rowData -> row.addCell(new TableRowCell(rowData)));
            if (asHeader.getAndSet(false)) {
                table.setHeaderRow(row);
            } else {
                table.addRow(row);
            }
        });

        System.out.println(table.getFormattedString());
    } catch (final IOException cause) {
        throw new RuntimeException("An error occurred whilst parsing stdin!", cause);
    }
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step1PrepareContainers.java

public static void main(String[] args) throws IOException {
    // queries with narratives in CSV
    File queries = new File(args[0]);
    File relevantInformationExamplesFile = new File(args[1]);

    Map<Integer, Map<Integer, List<String>>> relevantInformationMap = parseRelevantInformationFile(
            relevantInformationExamplesFile);

    // output dir
    File outputDir = new File(args[2]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();/*  w  ww  .  java  2s. c o  m*/
    }

    // iterate over queries
    CSVParser csvParser = CSVParser.parse(queries, Charset.forName("utf-8"), CSVFormat.DEFAULT);
    for (CSVRecord record : csvParser) {
        // create new container, fill, and store
        QueryResultContainer container = new QueryResultContainer();
        container.qID = record.get(0);
        container.query = record.get(1);

        // Fill some dummy text first
        container.relevantInformationExamples.addAll(Collections.singletonList("ERROR. Information missing."));
        container.irrelevantInformationExamples
                .addAll(Collections.singletonList("ERROR. Information missing."));

        // and now fill it with existing information if available
        Integer queryID = Integer.valueOf(container.qID);
        if (relevantInformationMap.containsKey(queryID)) {
            if (relevantInformationMap.get(queryID).containsKey(0)) {
                container.irrelevantInformationExamples = new ArrayList<>(
                        relevantInformationMap.get(queryID).get(0));
            }

            if (relevantInformationMap.get(queryID).containsKey(1)) {
                container.relevantInformationExamples = new ArrayList<>(
                        relevantInformationMap.get(queryID).get(1));
            }
        }

        File outputFile = new File(outputDir, container.qID + ".xml");
        FileUtils.writeStringToFile(outputFile, container.toXML());
        System.out.println("Finished " + outputFile);
    }
}

From source file:com.stratio.decision.executables.DataFlowFromCsvMain.java

public static void main(String[] args) throws IOException, NumberFormatException, InterruptedException {
    if (args.length < 4) {
        log.info(//from  w ww .  j a  v a2  s. c o m
                "Usage: \n param 1 - path to file \n param 2 - stream name to send the data \n param 3 - time in ms to wait to send each data \n param 4 - broker list");
    } else {
        Producer<String, String> producer = new Producer<String, String>(createProducerConfig(args[3]));
        Gson gson = new Gson();

        Reader in = new FileReader(args[0]);
        CSVParser parser = CSVFormat.DEFAULT.parse(in);

        List<String> columnNames = new ArrayList<>();
        for (CSVRecord csvRecord : parser.getRecords()) {

            if (columnNames.size() == 0) {
                Iterator<String> iterator = csvRecord.iterator();
                while (iterator.hasNext()) {
                    columnNames.add(iterator.next());
                }
            } else {
                StratioStreamingMessage message = new StratioStreamingMessage();

                message.setOperation(STREAM_OPERATIONS.MANIPULATION.INSERT.toLowerCase());
                message.setStreamName(args[1]);
                message.setTimestamp(System.currentTimeMillis());
                message.setSession_id(String.valueOf(System.currentTimeMillis()));
                message.setRequest_id(String.valueOf(System.currentTimeMillis()));
                message.setRequest("dummy request");

                List<ColumnNameTypeValue> sensorData = new ArrayList<>();
                for (int i = 0; i < columnNames.size(); i++) {

                    // Workaround
                    Object value = null;
                    try {
                        value = Double.valueOf(csvRecord.get(i));
                    } catch (NumberFormatException e) {
                        value = csvRecord.get(i);
                    }
                    sensorData.add(new ColumnNameTypeValue(columnNames.get(i), null, value));
                }

                message.setColumns(sensorData);

                String json = gson.toJson(message);
                log.info("Sending data: {}", json);
                producer.send(new KeyedMessage<String, String>(InternalTopic.TOPIC_DATA.getTopicName(),
                        STREAM_OPERATIONS.MANIPULATION.INSERT, json));

                log.info("Sleeping {} ms...", args[2]);
                Thread.sleep(Long.valueOf(args[2]));
            }
        }
        log.info("Program completed.");
    }
}

From source file:ch.bfh.unicert.certimport.Main.java

public static void main(String[] args) throws IOException, InvalidNameException {

    logger.info("File read");

    File f = new File(csvPath);
    CSVParser parser = CSVParser.parse(f, Charset.forName("UTF-8"), CSVFormat.DEFAULT);

    for (CSVRecord record : parser) {
        createCertificate(record);/*  w w w.j  a v  a  2  s  . com*/

    }

}

From source file:io.mindmaps.migration.csv.Main.java

public static void main(String[] args) {

    String csvFileName = null;//from   w  w w. ja  va2 s . c  o  m
    String csvEntityType = null;
    String engineURL = null;
    String graphName = null;

    for (int i = 0; i < args.length; i++) {
        if ("-file".equals(args[i]))
            csvFileName = args[++i];
        else if ("-graph".equals(args[i]))
            graphName = args[++i];
        else if ("-engine".equals(args[i]))
            engineURL = args[++i];
        else if ("-as".equals(args[i])) {
            csvEntityType = args[++i];
        } else if ("csv".equals(args[0])) {
            continue;
        } else
            die("Unknown option " + args[i]);
    }

    if (csvFileName == null) {
        die("Please specify CSV file using the -csv option");
    }
    File csvFile = new File(csvFileName);
    if (!csvFile.exists()) {
        die("Cannot find file: " + csvFileName);
    }
    if (graphName == null) {
        die("Please provide the name of the graph using -graph");
    }
    if (csvEntityType == null) {
        csvEntityType = csvFile.getName().replaceAll("[^A-Za-z0-9]", "_");
    }

    System.out.println("Migrating " + csvFileName + " using MM Engine "
            + (engineURL == null ? "local" : engineURL) + " into graph " + graphName);

    // perform migration
    CSVSchemaMigrator schemaMigrator = new CSVSchemaMigrator();
    CSVDataMigrator dataMigrator = new CSVDataMigrator();

    //
    try {
        MindmapsGraph graph = engineURL == null ? MindmapsClient.getGraph(graphName)
                : MindmapsClient.getGraph(graphName, engineURL);

        Loader loader = engineURL == null ? new BlockingLoader(graphName)
                : new DistributedLoader(graphName, Lists.newArrayList(engineURL));

        CSVParser csvParser = CSVParser.parse(csvFile.toURI().toURL(), StandardCharsets.UTF_8,
                CSVFormat.DEFAULT.withHeader());

        schemaMigrator.graph(graph).configure(csvEntityType, csvParser).migrate(loader);

        System.out.println("Schema migration successful");

        dataMigrator.graph(graph).configure(csvEntityType, csvParser).migrate(loader);

        System.out.println("DataType migration successful");

    } catch (Throwable throwable) {
        throwable.printStackTrace(System.err);
    }

    System.exit(0);
}

From source file:edu.caltech.ipac.firefly.server.util.DsvToDataGroup.java

public static void main(String[] args) {

    try {//from w w  w  .j  a  v  a 2s .  c  o m
        File inf = new File(args[0]);
        DataGroup dg = parse(inf, CSVFormat.DEFAULT);
        IpacTableWriter.save(System.out, dg);
        write(new File(inf.getAbsolutePath() + ".csv"), dg);
        write(new File(inf.getAbsolutePath() + ".tsv"), dg, CSVFormat.TDF);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.ctt.persistance.StorageFile.java

public static void saveTransactions(byte[] file) throws IOException {
    try {//from  ww  w.  j a  va2s .  c  o  m

        List<String> list = new ArrayList<String>();

        CSVParser csv;
        csv = CSVParser.parse(new String(file), CSVFormat.DEFAULT);

        List<CSVRecord> it = csv.getRecords();
        Transaction obj;

        int i = 0;
        for (i = 0; i < it.size(); i++) {
            if (i != 0) {
                obj = new Transaction((CSVRecord) it.get(i));

                if (obj.validateCanonical() && obj.isRepayment()) {
                    list.add(obj.repaymentString());

                    StorageFile.saveTransaction(obj);
                }
            }
        }
        FileUtils.writeLines(new File(Main.prop.getProperty("path_repayments")), list, "\n", true);
    } catch (Exception e) {
        Main.appendLog("Error: SAVE TRANSACTIONS: " + e.getMessage());
    }
}

From source file:com.lithium.flow.util.CsvFormats.java

@Nonnull
public static CSVFormat fromConfig(@Nonnull Config config) {
    checkNotNull(config);/*w ww  .  j  ava 2s .com*/
    switch (config.getString("csv.format", "default")) {
    case "default":
        return CSVFormat.DEFAULT;
    case "excel":
        return CSVFormat.EXCEL;
    case "mysql":
        return CSVFormat.MYSQL;
    case "rfc4180":
        return CSVFormat.RFC4180;
    case "tdf":
        return CSVFormat.TDF;
    case "custom":
        return CSVFormat.newFormat(getChar(config, "csv.delimiter", ','))
                .withAllowMissingColumnNames(getBoolean(config, "csv.allowMissingColumnNames"))
                .withCommentMarker(getChar(config, "csv.commentMarker"))
                .withEscape(getChar(config, "csv.escape")).withHeader(getHeader(config, "csv.header"))
                .withIgnoreEmptyLines(getBoolean(config, "csv.ignoreEmptyLines"))
                .withIgnoreSurroundingSpaces(getBoolean(config, "csv.ignoreSurroundingSpaces"))
                .withNullString(getString(config, "csv.nullString")).withQuote(getChar(config, "csv.quote"))
                .withQuoteMode(getQuoteMode(config, "csv.quoteMode"))
                .withRecordSeparator(getString(config, "csv.recordSeparator"))
                .withSkipHeaderRecord(getBoolean(config, "csv.skipHeaderRecord"));
    default:
        return CSVFormat.DEFAULT;
    }
}

From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.ConfusionMatrixTools.java

public static ConfusionMatrix tokenLevelPredictionsToConfusionMatrix(File predictionsFile) throws IOException {
    ConfusionMatrix cm = new ConfusionMatrix();

    CSVParser csvParser = new CSVParser(new FileReader(predictionsFile),
            CSVFormat.DEFAULT.withCommentMarker('#'));

    for (CSVRecord csvRecord : csvParser) {
        // update confusion matrix
        cm.increaseValue(csvRecord.get(0), csvRecord.get(1));
    }/*from  w w w . j  av  a 2 s.  co m*/

    return cm;
}