compile CSV Line - Java File Path IO

Java examples for File Path IO:CSV File

Description

compile CSV Line

Demo Code


//package com.java2s;

public class Main {
    static String compileCSVLine(String[] fields, int i, String prefix) {

        String line = (prefix == null) ? "" : prefix;
        for (; i < fields.length; i++) {
            if (line.length() > 0)
                line = line + ",";
            String f = fields[i];

            if (f.startsWith("\"") && f.endsWith("\"")) {
                f = f.substring(1, f.lastIndexOf('"'));
                f = f.replaceAll("\"", "'");
                f = '"' + f + '"';
            } else {
                f = f.replaceAll("\"", "'");
            }/*from  ww w  .  j a va 2 s.  c o  m*/

            f = f.replaceAll(",", " ");

            if (f.contains(",") && !f.startsWith("\"") && !f.endsWith("\"")) {
                f = '"' + f + '"';
            }

            line = line + f;
        }

        return line;
    }
}

Related Tutorials