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

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

Introduction

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

Prototype

public CSVFormat withDelimiter(final char delimiter) 

Source Link

Document

Sets the delimiter of the format to the specified character.

Usage

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

/**
 * @param args/*from   www .java2s. c o  m*/
 */
public static void main(String[] args) {
    if (args.length > 7 | args.length == 0 || !args[0].equals("-f") || !args[2].equals("-u")
            || !args[4].equals("-i")) {
        System.err.println(USAGE);
        System.exit(1);
    }
    String filename = args[1].trim();
    url = args[3].trim();
    needsPrefix = !args[5].equals("unique");
    isVerbose = (args.length == 7 && args[6].equals("-v"));
    System.out.println("Loading data from " + filename + " " + (needsPrefix ? "IDs will be prefixed " : " "));
    long start = System.currentTimeMillis();
    boolean isReading = false;
    CSVPrinter printer = null;

    CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',').withAllowMissingColumnNames(true);
    CSVParser parser;
    try {
        if (isVerbose) {
            printer = new CSVPrinter(System.err, format.withDelimiter('|'));
        }
        parser = CSVParser.parse(new File(filename), Charset.forName("UTF-8"), format);

        solrSrvr = SingletonSolrServer.getSolrServer(url);
        for (CSVRecord record : parser) {
            numRecs++;
            HashMap<String, String> recMap = new HashMap<String, String>();
            for (String field : FIELDS) {
                String value = null;
                try {
                    value = record.get(field);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().indexOf("expected one of") == -1) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
                value = value == null ? "" : value.trim();
                recMap.put(field, value);
            }
            String id = recMap.get("ID");
            if (id.isEmpty()) {
                if (isVerbose) {
                    System.err.println("Record missing ID: ");
                    printer.printRecord(record);
                }
            } else {
                String type = recMap.get("Type");
                SolrDocument sdoc = getDocFromSolr(recMap.get("ID"));
                try {
                    if (type.toLowerCase().equals("course")) {
                        processCourse(recMap, sdoc);
                        isReading = false;
                    } else {
                        if (!isReading) {
                            addUpdateCommit(); // just in case the preceeding course(s) are related
                        }
                        processReading(recMap, sdoc);
                        isReading = true;
                    }
                } catch (Exception e) {
                    if (isVerbose) {
                        System.err.println("Record # " + numRecs + " not used:\n\t" + e.getMessage());
                    }
                    errRecs++;
                }
            }
            if (beans.size() > 20) {
                addUpdateCommit();
            }
        }
        parser.close();
        if (beans.size() > 0 || docUpdates.size() > 0) {
            addUpdateCommit();
        }
    } catch (FileNotFoundException e) {
        System.err.println(filename + " not found");
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    long end = System.currentTimeMillis();
    long courseTime = (end - start) / (long) 1000;
    try {
        solrSrvr.optimize();
    } catch (SolrServerException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println(numRecs + " records found, of which " + errRecs + " had a problem; time: " + courseTime
            + " seconds " + ((courseTime > 60) ? ("(" + (courseTime / (long) 60) + " minutes)") : ""));
    System.exit(0);
}

From source file:com.anhth12.lambda.common.text.TextUtils.java

private static CSVFormat formatForDelimiter(char delimiter) {
    CSVFormat format = CSV_FORMAT;
    if (delimiter != format.getDelimiter()) {
        format = format.withDelimiter(delimiter);
    }//from www  .jav a  2  s  .  c  om
    return format;
}

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

private CSVFormat createCSVFormat(List<String> headers) {
    CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(false).withIgnoreSurroundingSpaces(true);
    if (myCsvOptions != null) {
        format = format.withDelimiter(myCsvOptions.sSeparatedChar.charAt(0))
                .withQuote(myCsvOptions.sSeparatedTextChar.charAt(0));
    }//from  w  ww. j a  v a2  s  . c  o m
    if (headers != null) {
        format = format.withHeader(headers.toArray(new String[0]));
    }
    return format;
}

From source file:com.apkcategorychecker.writer.WriterCSV.java

@Override
public void Write(AnalyzerResult result, String _csvPath, int _counter) {

    try {/*from   w ww .java2s.  c  o m*/

        ArrayList<AnalyzerResult> resultList = new ArrayList<AnalyzerResult>();
        resultList.add(result);

        /*--Create the CSVFormat object--*/

        CSVFormat format = CSVFormat.DEFAULT.withHeader();

        /*--Writing in a CSV file--*/

        FileWriter _out = new FileWriter(_csvPath, true);
        CSVPrinter printer;
        printer = new CSVPrinter(_out, format.withDelimiter('#'));

        /*--Retrieve APKResult and Write in file--*/

        Iterator<AnalyzerResult> it = resultList.iterator();
        while (it.hasNext()) {
            AnalyzerResult _resultElement = it.next();
            List<String> resultData = new ArrayList<>();
            resultData.add(String.valueOf(_counter));
            resultData.add(_resultElement.get_APKName());
            resultData.add(_resultElement.get_APKPath());
            resultData.add(_resultElement.get_Package());
            resultData.add(_resultElement.get_APKMainFramework());
            resultData.add(_resultElement.get_APKBaseFramework());
            resultData.add(String.valueOf(_resultElement.get_html()));
            resultData.add(String.valueOf(_resultElement.get_js()));
            resultData.add(String.valueOf(_resultElement.get_css()));
            resultData.add(_resultElement.get_debuggable());
            resultData.add(_resultElement.get_permissions());
            resultData.add(_resultElement.get_minSdkVersion());
            resultData.add(_resultElement.get_maxSdkVersion());
            resultData.add(_resultElement.get_targetSdkVersion());
            resultData.add(_resultElement.get_fileSize());
            resultData.add(String.valueOf(_resultElement.get_startAnalysis()));
            resultData.add(String.valueOf(_resultElement.get_durationAnalysis()));
            resultData.add(String.valueOf(_resultElement.get_decodeSuccess()));
            printer.printRecord(resultData);
        }

        /*--Close the printer--*/
        printer.close();
        System.out.println("Record added to CSV file");
        this.removeBlankLines(_csvPath);

    } catch (IOException ex) {
        Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.apkcategorychecker.writer.WriterCSV.java

@Override
public void createHeader(String _csvPath) throws IOException {
    /*--Create the CSVFormat object--*/

    CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',');

    /*--Writing in a CSV file--*/

    File _fileCSV = new File(_csvPath);
    FileWriter _out;//from  w w  w .  ja v  a2  s. c om
    _out = new FileWriter(_fileCSV);
    CSVPrinter printer;
    printer = new CSVPrinter(_out, format.withDelimiter('#'));
    System.out.println("Creating the CSV file....");
    try {
        printer.printRecord("App_ID", "APK_File_Name", "APK_File_Path", "APK_Package", "Main_Framework",
                "Base_Framework", "HTML", "JS", "CSS", "Android_Debuggable", "Android_Permissions",
                "Android_MinSdkVersion", "Android_MaxSdkVersion", "Android_TargetSdkVersion",
                "File_Size(Bytes)", "Start_Analysis_Time(milliseconds)", "Duration_Analysis_Time(milliseconds)",
                "Decode_Success");
    } catch (IOException ex) {
        Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
    }
    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  www .  j a v a  2s.  c om*/
    if (myCsvOptions.sSeparatedTextChar.length() == 1) {
        format = format.withQuote(myCsvOptions.sSeparatedTextChar.charAt(0));
    }

    return format;
}

From source file:com.apkcategorychecker.json.DefaultJsCSVBuilder.java

@Override
public void BuildJs(ArrayList<JsElement> jsList, String _destinationPath, String time) {

    try {/*from  w  w w . ja  v a 2  s  . c  o  m*/

        /*Check if _destinationPath is an APK or a Directory in order to
        *get the right destination path
        */

        File Destination = new File(_destinationPath);
        if (!Destination.exists()) {
            Destination.mkdir();
        }
        if (Destination.isDirectory()) {
            this._destPath = _destinationPath;
        } else if (Destination.isFile()) {
            this._destPath = _destinationPath.substring(0, _destinationPath.length() - 4);
        }

        /*--Create the CSVFormat object--*/

        CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',');

        /*--Writing in a CSV file--*/

        File _fileCSV = new File(_destPath + "/Results-JS FILES_" + time + ".csv");
        FileWriter _out = new FileWriter(_fileCSV);
        CSVPrinter printer;
        printer = new CSVPrinter(_out, format.withDelimiter('#'));
        System.out.println("Creating " + "Results-JS FILES_" + time + ".csv ....");
        try {
            printer.printRecord("appID", "jsFiles");
        } catch (IOException ex) {
            Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
        }

        /*--Retrieve APKResult and Write in file--*/

        @SuppressWarnings("unused")
        int i = 0;
        Iterator<JsElement> it = jsList.iterator();
        while (it.hasNext()) {
            JsElement _resultElement = it.next();
            List<String> resultData = new ArrayList<>();
            resultData.add(_resultElement.get_Id());
            resultData.add(_resultElement.get_JsFiles());
            i++;
            printer.printRecord(resultData);
        }

        /*--Close the printer--*/
        printer.close();
        System.out.println("Results-JS FILES_" + time + ".csv created");

    } catch (IOException ex) {
        Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:net.sourceforge.ganttproject.io.GanttCSVExport.java

/**
 * Save the project as CSV on a stream//  w ww. jav a2 s .  c  o m
 *
 * @throws IOException
 */
public void save(OutputStream stream) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(stream);
    CSVFormat format = CSVFormat.DEFAULT.withEscape('\\');
    if (csvOptions.sSeparatedChar.length() == 1) {
        format = format.withDelimiter(csvOptions.sSeparatedChar.charAt(0));
    }
    if (csvOptions.sSeparatedTextChar.length() == 1) {
        format = format.withEncapsulator(csvOptions.sSeparatedTextChar.charAt(0));
    }

    CSVPrinter csvPrinter = new CSVPrinter(writer, format);

    if (csvOptions.bFixedSize) {
        // TODO The CVS library we use is lacking support for fixed size
        getMaxSize();
    }

    writeTasks(csvPrinter);

    if (myProject.getHumanResourceManager().getResources().size() > 0) {
        csvPrinter.println();
        csvPrinter.println();
        writeResources(csvPrinter);
    }
    writer.flush();
    writer.close();
}

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

private CSVFormat createCSVFormat(String sampleData) throws IOException {
    CSVFormat format;
    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 {//from w ww  .  ja  v a2 s  .  co m
        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.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java

/**
 * Parses a sample file to allow schema specification when creating a new feed.
 *
 * @param sampleText the sample text//from   w  ww  .  j  av  a  2  s. c o  m
 * @return A configured parser
 * @throws IOException If there is an error parsing the sample file
 */
public CSVFormat detectCSVFormat(String sampleText, boolean headerRow, String seperatorStr) throws IOException {
    CSVFormat format = CSVFormat.DEFAULT.withAllowMissingColumnNames();
    Character separatorChar = null;
    if (StringUtils.isNotBlank(seperatorStr)) {
        separatorChar = seperatorStr.charAt(0);
    }
    try (BufferedReader br = new BufferedReader(new StringReader(sampleText))) {
        List<LineStats> lineStats = generateStats(br, separatorChar);
        Character quote = guessQuote(lineStats);
        Character delim = guessDelimiter(lineStats, sampleText, quote, headerRow);
        if (delim == null) {
            throw new IOException("Unrecognized format");
        }
        format = format.withDelimiter(delim);
        format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(quote);
    }
    return format;
}