Android Open Source - texthem C S V Writer






From Project

Back to project page texthem.

License

The source code is released under:

GNU General Public License

If you think the Android project texthem listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package au.com.bytecode.opencsv;
//from  ww  w.ja  v a2s . c o  m
/**
 Copyright 2005 Bytecode Pty Ltd.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * A very simple CSV writer released under a commercial-friendly license.
 *
 * @author Glen Smith
 */
public class CSVWriter implements Closeable, Flushable {

    public static final int INITIAL_STRING_SIZE = 128;
    /**
     * The character used for escaping quotes.
     */
    public static final char DEFAULT_ESCAPE_CHARACTER = '"';
    /**
     * The default separator to use if none is supplied to the constructor.
     */
    public static final char DEFAULT_SEPARATOR = ',';
    /**
     * The default quote character to use if none is supplied to the
     * constructor.
     */
    public static final char DEFAULT_QUOTE_CHARACTER = '"';
    /**
     * The quote constant to use when you wish to suppress all quoting.
     */
    public static final char NO_QUOTE_CHARACTER = '\u0000';
    /**
     * The escape constant to use when you wish to suppress all escaping.
     */
    public static final char NO_ESCAPE_CHARACTER = '\u0000';
    /**
     * Default line terminator uses platform encoding.
     */
    public static final String DEFAULT_LINE_END = "\n";
    private Writer rawWriter;
    private PrintWriter pw;
    private char separator;
    private char quotechar;
    private char escapechar;
    private String lineEnd;
    private ResultSetHelper resultService = new ResultSetHelperService();

    /**
     * Constructs CSVWriter using a comma for the separator.
     *
     * @param writer the writer to an underlying CSV source.
     */
    public CSVWriter(Writer writer) {
        this(writer, DEFAULT_SEPARATOR);
    }

    /**
     * Constructs CSVWriter with supplied separator.
     *
     * @param writer    the writer to an underlying CSV source.
     * @param separator the delimiter to use for separating entries.
     */
    public CSVWriter(Writer writer, char separator) {
        this(writer, separator, DEFAULT_QUOTE_CHARACTER);
    }

    /**
     * Constructs CSVWriter with supplied separator and quote char.
     *
     * @param writer    the writer to an underlying CSV source.
     * @param separator the delimiter to use for separating entries
     * @param quotechar the character to use for quoted elements
     */
    public CSVWriter(Writer writer, char separator, char quotechar) {
        this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER);
    }

    /**
     * Constructs CSVWriter with supplied separator and quote char.
     *
     * @param writer     the writer to an underlying CSV source.
     * @param separator  the delimiter to use for separating entries
     * @param quotechar  the character to use for quoted elements
     * @param escapechar the character to use for escaping quotechars or escapechars
     */
    public CSVWriter(Writer writer, char separator, char quotechar, char escapechar) {
        this(writer, separator, quotechar, escapechar, DEFAULT_LINE_END);
    }


    /**
     * Constructs CSVWriter with supplied separator and quote char.
     *
     * @param writer    the writer to an underlying CSV source.
     * @param separator the delimiter to use for separating entries
     * @param quotechar the character to use for quoted elements
     * @param lineEnd   the line feed terminator to use
     */
    public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd) {
        this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
    }


    /**
     * Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
     *
     * @param writer     the writer to an underlying CSV source.
     * @param separator  the delimiter to use for separating entries
     * @param quotechar  the character to use for quoted elements
     * @param escapechar the character to use for escaping quotechars or escapechars
     * @param lineEnd    the line feed terminator to use
     */
    public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
        this.rawWriter = writer;
        this.pw = new PrintWriter(writer);
        this.separator = separator;
        this.quotechar = quotechar;
        this.escapechar = escapechar;
        this.lineEnd = lineEnd;
    }

    /**
     * Writes the entire list to a CSV file. The list is assumed to be a
     * String[]
     *
     * @param allLines         a List of String[], with each String[] representing a line of
     *                         the file.
     * @param applyQuotesToAll true if all values are to be quoted.  false if quotes only
     *                         to be applied to values which contain the separator, escape,
     *                         quote or new line characters.
     */
    public void writeAll(List<String[]> allLines, boolean applyQuotesToAll) {
        for (String[] line : allLines) {
            writeNext(line, applyQuotesToAll);
        }
    }

    /**
     * Writes the entire list to a CSV file. The list is assumed to be a
     * String[]
     *
     * @param allLines a List of String[], with each String[] representing a line of
     *                 the file.
     */
    public void writeAll(List<String[]> allLines) {
        for (String[] line : allLines) {
            writeNext(line);
        }
    }

    protected void writeColumnNames(ResultSet rs)
            throws SQLException {

        writeNext(resultService.getColumnNames(rs));
    }

    /**
     * Writes the entire ResultSet to a CSV file.
     * <p/>
     * The caller is responsible for closing the ResultSet.
     *
     * @param rs                 the recordset to write
     * @param includeColumnNames true if you want column names in the output, false otherwise
     * @throws java.io.IOException   thrown by getColumnValue
     * @throws java.sql.SQLException thrown by getColumnValue
     */
    public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames) throws SQLException, IOException {
        writeAll(rs, includeColumnNames, false);
    }

    /**
     * Writes the entire ResultSet to a CSV file.
     * <p/>
     * The caller is responsible for closing the ResultSet.
     *
     * @throws java.io.IOException   thrown by getColumnValue
     * @throws java.sql.SQLException thrown by getColumnValue
     */
    public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim) throws SQLException, IOException {


        if (includeColumnNames) {
            writeColumnNames(rs);
        }

        while (rs.next()) {
            writeNext(resultService.getColumnValues(rs, trim));
        }
    }

    /**
     * Writes the next line to the file.
     *
     * @param nextLine         a string array with each comma-separated element as a separate
     *                         entry.
     * @param applyQuotesToAll true if all values are to be quoted.  false applies quotes only
     *                         to values which contain the separator, escape, quote or new line characters.
     */
    public void writeNext(String[] nextLine, boolean applyQuotesToAll) {

        if (nextLine == null)
            return;

        StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
        for (int i = 0; i < nextLine.length; i++) {

            if (i != 0) {
                sb.append(separator);
            }

            String nextElement = nextLine[i];

            if (nextElement == null)
                continue;

            Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);

            if ((applyQuotesToAll || stringContainsSpecialCharacters) && quotechar != NO_QUOTE_CHARACTER)
                sb.append(quotechar);

            if (stringContainsSpecialCharacters) {
                sb.append(processLine(nextElement));
            } else {
                sb.append(nextElement);
            }

            if ((applyQuotesToAll || stringContainsSpecialCharacters) && quotechar != NO_QUOTE_CHARACTER)
                sb.append(quotechar);
        }

        sb.append(lineEnd);
        pw.write(sb.toString());
    }

    /**
     * Writes the next line to the file.
     *
     * @param nextLine a string array with each comma-separated element as a separate
     *                 entry.
     */
    public void writeNext(String[] nextLine) {
        writeNext(nextLine, true);
    }

    private boolean stringContainsSpecialCharacters(String line) {
        return line.indexOf(quotechar) != -1 || line.indexOf(escapechar) != -1 || line.indexOf(separator) != -1 || line.contains("\n") || line.contains("\r");
    }

    protected StringBuilder processLine(String nextElement) {
        StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
        for (int j = 0; j < nextElement.length(); j++) {
            char nextChar = nextElement.charAt(j);
            if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
                sb.append(escapechar).append(nextChar);
            } else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
                sb.append(escapechar).append(nextChar);
            } else {
                sb.append(nextChar);
            }
        }

        return sb;
    }

    /**
     * Flush underlying stream to writer.
     *
     * @throws IOException if bad things happen
     */
    public void flush() throws IOException {

        pw.flush();

    }

    /**
     * Close the underlying stream writer flushing any buffered content.
     *
     * @throws IOException if bad things happen
     */
    public void close() throws IOException {
        flush();
        pw.close();
        rawWriter.close();
    }

    /**
     * Checks to see if the there has been an error in the printstream.
     */
    public boolean checkError() {
        return pw.checkError();
    }

    public void setResultService(ResultSetHelper resultService) {
        this.resultService = resultService;
    }

    public void flushQuietly() {
        try {
            flush();
        } catch (IOException e) {
            // catch exception and ignore.
        }
    }
}




Java Source Code List

a2.marketingsms.ApplicationTest.java
a2.marketingsms.SelectList.java
a2.marketingsms.Texthem.java
a2.marketingsms.components.ContactImporter.java
a2.marketingsms.components.DialogHandler.java
a2.marketingsms.components.MyProgressDialog.java
a2.marketingsms.components.SMSSender.java
a2.marketingsms.model.Contact.java
a2.marketingsms.model.TemplateText.java
au.com.bytecode.opencsv.CSVIterator.java
au.com.bytecode.opencsv.CSVParserBuilder.java
au.com.bytecode.opencsv.CSVParser.java
au.com.bytecode.opencsv.CSVReaderBuilder.java
au.com.bytecode.opencsv.CSVReader.java
au.com.bytecode.opencsv.CSVWriter.java
au.com.bytecode.opencsv.ResultSetHelperService.java
au.com.bytecode.opencsv.ResultSetHelper.java
com.danielme.blog.demo.listviewcheckbox.CustomArrayAdapter.java
com.danielme.blog.demo.listviewcheckbox.DontPressWhenPressParentCheckBox.java
com.danielme.blog.demo.listviewcheckbox.Row.java
filechooser.FileArrayAdapter.java
filechooser.FileChooser.java
filechooser.Option.java
org.mozilla.universalchardet.CharsetListener.java
org.mozilla.universalchardet.Constants.java
org.mozilla.universalchardet.UniversalDetector.java
org.mozilla.universalchardet.prober.Big5Prober.java
org.mozilla.universalchardet.prober.CharsetProber.java
org.mozilla.universalchardet.prober.EUCJPProber.java
org.mozilla.universalchardet.prober.EUCKRProber.java
org.mozilla.universalchardet.prober.EUCTWProber.java
org.mozilla.universalchardet.prober.EscCharsetProber.java
org.mozilla.universalchardet.prober.GB18030Prober.java
org.mozilla.universalchardet.prober.HebrewProber.java
org.mozilla.universalchardet.prober.Latin1Prober.java
org.mozilla.universalchardet.prober.MBCSGroupProber.java
org.mozilla.universalchardet.prober.SBCSGroupProber.java
org.mozilla.universalchardet.prober.SJISProber.java
org.mozilla.universalchardet.prober.SingleByteCharsetProber.java
org.mozilla.universalchardet.prober.UTF8Prober.java
org.mozilla.universalchardet.prober.contextanalysis.EUCJPContextAnalysis.java
org.mozilla.universalchardet.prober.contextanalysis.JapaneseContextAnalysis.java
org.mozilla.universalchardet.prober.contextanalysis.SJISContextAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.Big5DistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.CharDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCJPDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCKRDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.EUCTWDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.GB2312DistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.JISDistributionAnalysis.java
org.mozilla.universalchardet.prober.distributionanalysis.SJISDistributionAnalysis.java
org.mozilla.universalchardet.prober.sequence.BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.CyrillicModel.java
org.mozilla.universalchardet.prober.sequence.GreekModel.java
org.mozilla.universalchardet.prober.sequence.HebrewModel.java
org.mozilla.universalchardet.prober.sequence.Ibm855Model.java
org.mozilla.universalchardet.prober.sequence.Ibm866Model.java
org.mozilla.universalchardet.prober.sequence.Koi8rModel.java
org.mozilla.universalchardet.prober.sequence.Latin5BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.Latin5Model.java
org.mozilla.universalchardet.prober.sequence.Latin7Model.java
org.mozilla.universalchardet.prober.sequence.MacCyrillicModel.java
org.mozilla.universalchardet.prober.sequence.SequenceModel.java
org.mozilla.universalchardet.prober.sequence.Win1251BulgarianModel.java
org.mozilla.universalchardet.prober.sequence.Win1251Model.java
org.mozilla.universalchardet.prober.sequence.Win1253Model.java
org.mozilla.universalchardet.prober.statemachine.Big5SMModel.java
org.mozilla.universalchardet.prober.statemachine.CodingStateMachine.java
org.mozilla.universalchardet.prober.statemachine.EUCJPSMModel.java
org.mozilla.universalchardet.prober.statemachine.EUCKRSMModel.java
org.mozilla.universalchardet.prober.statemachine.EUCTWSMModel.java
org.mozilla.universalchardet.prober.statemachine.GB18030SMModel.java
org.mozilla.universalchardet.prober.statemachine.HZSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022CNSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022JPSMModel.java
org.mozilla.universalchardet.prober.statemachine.ISO2022KRSMModel.java
org.mozilla.universalchardet.prober.statemachine.PkgInt.java
org.mozilla.universalchardet.prober.statemachine.SJISSMModel.java
org.mozilla.universalchardet.prober.statemachine.SMModel.java
org.mozilla.universalchardet.prober.statemachine.UCS2BESMModel.java
org.mozilla.universalchardet.prober.statemachine.UTF8SMModel.java