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:gov.usda.DataCatalogClient.Catalog.java

/**
 * Outputs a Catalog into tab delimited format.
 * <p>//from www .  j av a 2 s  . co  m
 * Begins the process by listing out the header and calling all datasets to create
 * tab delimitted lines.
 * @param filePath String The output file for the catalog tab delimitted file.
 * @param dataListCode DataListingCode This will either print the Public Data Listing or the Enterprise 
 * Data Inventory
 */
public void toCSV(String filePath, DataListingCode dataListingCode) throws IOException {
    Collections.sort(dataSetList);
    if (filePath == null) {
        throw (new NullPointerException("filepath cannot be null"));
    }
    PrintWriter out = null;
    CSVPrinter csvPrinter = null;
    CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
    List<String> catalogString = new ArrayList<String>();
    catalogString.add("Agency Name");
    catalogString.add("Title");
    catalogString.add("Description");
    catalogString.add("MediaType");
    catalogString.add("Download URL");
    catalogString.add("Frequency");
    catalogString.add("Bureau Code");
    catalogString.add("Contact Email");
    catalogString.add("Contact Name");
    catalogString.add("Landing Page");
    catalogString.add("Program Code");
    catalogString.add("Publisher");
    catalogString.add("Public Access Level");
    catalogString.add("Access Level Comment");
    catalogString.add("Tags");
    catalogString.add("Last Update");
    catalogString.add("Release Date");
    catalogString.add("Unique Identifier");
    catalogString.add("Data Dictionary");
    catalogString.add("License");
    catalogString.add("Spatial");
    catalogString.add("Temporal");
    catalogString.add("System of Records");
    catalogString.add("Data Quality");
    catalogString.add("Language");
    catalogString.add("Theme");
    catalogString.add("Reference");
    catalogString.add("CKAN Create Date");
    catalogString.add("CKAN Modified Date");
    catalogString.add("CKAN Revision Timestamp");
    catalogString.add("Is Part Of");

    try {
        out = new PrintWriter(filePath);
        csvPrinter = new CSVPrinter(out, csvFormat);
        csvPrinter.printRecord(catalogString);
        for (Dataset ds : dataSetList) {
            if (dataListingCode.equals(DataListingCode.ENTERPRISE_DATA_INVENTORY)) {
                csvPrinter.printRecord(ds.datasetToListString());
            } else if (dataListingCode.equals(DataListingCode.PUBLIC_DATA_LISTING)) {
                if (ds.getAccessLevel().equals(Dataset.AccessLevel.PUBLIC.toString())
                        || ds.getAccessLevel().equals(Dataset.AccessLevel.RESTRICTED)) {
                    csvPrinter.printRecord(ds.datasetToListString());
                }
            }
        }
    } catch (IOException e) {
        throw (e);
    } finally {
        out.close();
        csvPrinter.close();
    }
}

From source file:com.team3637.service.ScheduleServiceMySQLImpl.java

@Override
public void importCSV(String inputFile) {
    try {//from  ww  w .j ava2s.c o m
        String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
        csvData = csvData.replaceAll("\\r", "");
        CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (CSVRecord record : parser) {
            Schedule schedule = new Schedule();
            schedule.setId(Integer.parseInt(record.get(0)));
            schedule.setMatchNum(Integer.parseInt(record.get(1)));
            schedule.setB1(Integer.parseInt(record.get(2)));
            schedule.setB2(Integer.parseInt(record.get(3)));
            schedule.setB3(Integer.parseInt(record.get(4)));
            schedule.setR1(Integer.parseInt(record.get(5)));
            schedule.setR2(Integer.parseInt(record.get(6)));
            schedule.setR3(Integer.parseInt(record.get(7)));
            if (checkForMatch(schedule))
                update(schedule);
            else
                create(schedule);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.inren.service.banking.BankDataServiceImpl.java

private CSVFormat getIngDibaCsvFormat() {
    return CSVFormat.DEFAULT.withDelimiter(';');
}

From source file:eu.verdelhan.ta4j.indicators.trackers.ParabolicSarIndicatorTest.java

private Iterable<CSVRecord> getCsvRecords(String fileName) throws IOException {
    InputStreamReader priceCSVReader = new InputStreamReader(this.getClass().getResourceAsStream(fileName));

    return CSVFormat.DEFAULT.parse(priceCSVReader);
}

From source file:edu.caltech.ipac.firefly.server.catquery.SDSSQuery.java

@Override
protected File loadDataFile(TableServerRequest request) throws IOException, DataAccessException {

    File outFile;/*from   w  w w .j  av  a 2  s  . co  m*/
    try {
        // votable has utf-16 encoding, which mismatches the returned content type
        // this confuses tools
        File csv = createFile(request, ".csv");
        String uploadFname = request.getParam(SDSSRequest.FILE_NAME);
        if (StringUtils.isEmpty(uploadFname)) {

            URL url = createGetURL(request);

            URLConnection conn = URLDownload.makeConnection(url);
            conn.setRequestProperty("Accept", "*/*");

            URLDownload.getDataToFile(conn, csv);
        } else {
            File uploadFile = ServerContext.convertToFile(uploadFname);
            File sdssUFile;
            if (uploadFile.canRead()) {
                sdssUFile = getSDSSUploadFile(uploadFile);
            } else {
                throw new EndUserException("SDSS catalog search failed",
                        "Can not read uploaded file: " + uploadFname);
            }

            URL url = new URL(SERVICE_URL_UPLOAD);
            // use uploadFname
            //POST http://skyserver.sdss3.org/public/en/tools/crossid/x_crossid.aspx

            _postBuilder = new MultiPartPostBuilder(url.toString());
            if (_postBuilder == null) {
                throw new EndUserException("Failed to create HTTP POST request", "URL " + SERVICE_URL_UPLOAD);
            }

            insertPostParams(request, sdssUFile);
            BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(csv), 10240);
            MultiPartPostBuilder.MultiPartRespnse resp = null;
            try {
                resp = _postBuilder.post(writer);
            } finally {
                writer.close();
            }

            if (resp == null) {
                throw new IOException("Exception during post");
            } else if (resp.getStatusCode() < 200 || resp.getStatusCode() > 300) {
                // throw new IOException(resp.getStatusMsg());
                // try repeating the request through CasJobs
                boolean nearestOnly = request.getBooleanParam(SDSSRequest.NEAREST_ONLY);
                String radiusArcMin = request.getParam(SDSSRequest.RADIUS_ARCMIN);
                SDSSCasJobs.getCrossMatchResults(sdssUFile, nearestOnly, radiusArcMin, csv);
            }
        }

        // check for errors in returned file
        evaluateCVS(csv);

        DataGroup dg = DsvToDataGroup.parse(csv, CSVFormat.DEFAULT.withCommentStart('#'));
        if (dg == null) {
            _log.briefInfo("no data found for search");
            return null;
        }

        /*
        //TG No need to decrement up_id, since we are using the original upload id
        if (!StringUtils.isEmpty(uploadFname) && dg.containsKey("up_id")) {
        // increment up_id(uploaded id) by 1 if it's an multi object search
        DataType upId = dg.getDataDefintion("up_id");
        for(DataObject row : dg) {
            int id = StringUtils.getInt(String.valueOf(row.getDataElement(upId)), -1);
            if (id >= 0) {
                row.setDataElement(upId, id + 1);
            }
        }
        }
        */

        outFile = createFile(request, ".tbl");
        IpacTableWriter.save(outFile, dg);

    } catch (MalformedURLException e) {
        _log.error(e, "Bad URL");
        throw makeException(e, "SDSS Catalog Query Failed - bad url");
    } catch (IOException e) {
        _log.error(e, e.toString());
        throw makeException(e, "SDSS Catalog Query Failed - network Error");
    } catch (EndUserException e) {
        _log.error(e, e.toString());
        throw makeException(e, "SDSS Catalog Query Failed - network Error");
    } catch (Exception e) {
        _log.error(e, e.toString());
        throw makeException(e, "SDSS Catalog Query Failed");
    }
    return outFile;
}

From source file:javalibs.CSVDataNormalizer.java

private void readCSV() {
    try {//from  www  . j  a v  a 2s. co  m
        CSVParser parser = new CSVParser(Files.newBufferedReader(Paths.get(this.csvPath)),
                CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim());

        // Get all headers in the CSV file so they can be used later when writing the file
        this.headerMap = parser.getHeaderMap();

        // Add them to the records list for later use
        this.allRecords = parser.getRecords();

        parser.close();

        reverseHeaderMap();
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:com.ggvaidya.scinames.ui.DatasetImporterController.java

private Dataset loadDataset() throws IOException {
    String format = fileFormatComboBox.getSelectionModel().getSelectedItem();
    CSVFormat csvFormat = null;//from   www . j  a  va 2  s .  c  o m
    if (format == null) {
        csvFormat = CSVFormat.DEFAULT;
    } else {
        switch (format) {
        case "List of names":
            return Checklist.fromListInFile(currentFile);
        case "Default CSV":
            csvFormat = CSVFormat.DEFAULT;
            break;
        case "Microsoft Excel CSV":
            csvFormat = CSVFormat.EXCEL;
            break;
        case "RFC 4180 CSV":
            csvFormat = CSVFormat.RFC4180;
            break;
        case "Oracle MySQL CSV":
            csvFormat = CSVFormat.MYSQL;
            break;
        case "Tab-delimited file":
            csvFormat = CSVFormat.TDF;
            break;
        case "TaxDiff file":
            return ChecklistDiff.fromTaxDiffFile(currentFile);
        case "Excel file":
            return new ExcelImporter(currentFile).asDataset(0);
        }
    }

    if (csvFormat == null) {
        LOGGER.info("Could not determine CSV format from format '" + format + "', using CSV default.");
        csvFormat = CSVFormat.DEFAULT;
    }

    return Dataset.fromCSV(csvFormat, currentFile);
}

From source file:edu.harvard.mcz.dwcaextractor.DwCaExtractor.java

/**
 * Setup conditions to run./*from w w w  .jav  a 2s. c  o m*/
 * 
 * @param args command line arguments
 * @return true if setup was successful, false otherwise.
 */
protected boolean setup(String[] args) {
    boolean setupOK = false;
    CmdLineParser parser = new CmdLineParser(this);
    //parser.setUsageWidth(4096);
    try {
        parser.parseArgument(args);

        if (help) {
            parser.printUsage(System.out);
            System.exit(0);
        }

        if (archiveFilePath != null) {
            String filePath = archiveFilePath;
            logger.debug(filePath);
            File file = new File(filePath);
            if (!file.exists()) {
                // Error
                logger.error(filePath + " not found.");
            }
            if (!file.canRead()) {
                // error
                logger.error("Unable to read " + filePath);
            }
            if (file.isDirectory()) {
                // check if it is an unzipped dwc archive.
                dwcArchive = openArchive(file);
            }
            if (file.isFile()) {
                // unzip it
                File outputDirectory = new File(file.getName().replace(".", "_") + "_content");
                if (!outputDirectory.exists()) {
                    outputDirectory.mkdir();
                    try {
                        byte[] buffer = new byte[1024];
                        ZipInputStream inzip = new ZipInputStream(new FileInputStream(file));
                        ZipEntry entry = inzip.getNextEntry();
                        while (entry != null) {
                            String fileName = entry.getName();
                            File expandedFile = new File(outputDirectory.getPath() + File.separator + fileName);
                            new File(expandedFile.getParent()).mkdirs();
                            FileOutputStream expandedfileOutputStream = new FileOutputStream(expandedFile);
                            int len;
                            while ((len = inzip.read(buffer)) > 0) {
                                expandedfileOutputStream.write(buffer, 0, len);
                            }

                            expandedfileOutputStream.close();
                            entry = inzip.getNextEntry();
                        }
                        inzip.closeEntry();
                        inzip.close();
                        logger.debug("Unzipped archive into " + outputDirectory.getPath());
                    } catch (FileNotFoundException e) {
                        logger.error(e.getMessage());
                    } catch (IOException e) {
                        logger.error(e.getMessage(), e);
                    }
                }
                // look into the unzipped directory
                dwcArchive = openArchive(outputDirectory);
            }
            if (dwcArchive != null) {
                if (checkArchive()) {
                    // Check output 
                    csvPrinter = new CSVPrinter(new FileWriter(outputFilename, append),
                            CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC));
                    // no exception thrown
                    setupOK = true;
                }
            } else {
                System.out.println("Problem opening archive.");
                logger.error("Unable to unpack archive file.");
            }
            logger.debug(setupOK);
        }

    } catch (CmdLineException e) {
        logger.error(e.getMessage());
        parser.printUsage(System.err);
    } catch (IOException e) {
        logger.error(e.getMessage());
        System.out.println(e.getMessage());
        parser.printUsage(System.err);
    }
    return setupOK;
}

From source file:com.team3637.service.TeamServiceMySQLImpl.java

@Override
public void exportCSV(String outputFile) {
    List<Team> data = getTeams();
    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;// w w  w . ja  v  a  2  s  .  co  m
    try {
        fileWriter = new FileWriter(outputFile);
        csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (Team team : data) {
            List<Object> line = new ArrayList<>();
            for (Field field : Team.class.getDeclaredFields()) {
                field.setAccessible(true);
                Object value = field.get(team);
                line.add(value);
            }
            csvFilePrinter.printRecord(line);
        }
    } catch (IOException | IllegalAccessException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
            if (csvFilePrinter != null) {
                csvFilePrinter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.harvard.mcz.imagecapture.RunnableJobReportDialog.java

protected void serializeTableModel() {
    PrintWriter out = null;/*  ww  w. ja  v  a2  s  .  co m*/
    CSVPrinter writer = null;
    try {
        int cols = jTable.getModel().getColumnCount();
        CSVFormat csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                .withHeaderComments(jTextArea.getText());
        TableModel model = jTable.getModel();
        switch (cols) {
        case 9:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4), model.getColumnName(5),
                            model.getColumnName(6), model.getColumnName(7), model.getColumnName(8))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 6:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4), model.getColumnName(5))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 5:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 4:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        }

        log.debug(jTextArea.getText());
        log.debug(csvFormat.getHeaderComments());

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd_HHmmss");
        String time = dateFormat.format(now);
        String filename = "jobreport_" + time + ".csv";
        out = new PrintWriter(filename);

        writer = new CSVPrinter(out, csvFormat);
        writer.flush();

        int rows = jTable.getModel().getRowCount();
        for (int i = 0; i < rows; i++) {
            ArrayList<String> values = new ArrayList<String>();
            for (int col = 0; col < cols; col++) {
                values.add((String) jTable.getModel().getValueAt(i, col));
            }

            writer.printRecord(values);
        }
        writer.flush();
        writer.close();
        JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                "Saved report to file: " + filename, "Report to CSV file", JOptionPane.OK_OPTION);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        try {
            writer.close();
        } catch (Exception e) {
        }

    }
}