How to Read CSV Files in Java

How to Read CSV Files in Java

http://ift.tt/2qahcox

import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.ArrayList;
    static final private int NUMMARK = 10;
    static final private char COMMA = ',';
    static final private char DQUOTE = '"';
    static final private char CRETURN = '\r';
    static final private char LFEED = '\n';
    static final private char SQUOTE = '\'';
    static final private char COMMENT = '#';
    private boolean stripMultipleNewlines;
    private ArrayList < String > fields;
    static public Reader stripBom(InputStream in ) throws java.io.IOException, java.io.UnsupportedEncodingException {
        PushbackInputStream pin = new PushbackInputStream( in , 3);
        int len = pin.read(b, 0, b.length);
        if ((b[0] & 0xFF) == 0xEF && len == 3) {
            if ((b[1] & 0xFF) == 0xBB &&
                return new InputStreamReader(pin, "UTF-8");
            if ((b[0] & 0xFF) == 0xFE &&
                return new InputStreamReader(pin, "UTF-16BE");
            } else if ((b[0] & 0xFF) == 0xFF &&
                return new InputStreamReader(pin, "UTF-16LE");
        return new InputStreamReader(pin, "UTF-8");
    public CSV(boolean stripMultipleNewlines, char separator, Reader input) {
        this.stripMultipleNewlines = stripMultipleNewlines;
        this.separator = separator;
        this.fields = new ArrayList < String > ();
        this.in = new BufferedReader(input);
    public CSV(boolean stripMultipleNewlines, char separator, InputStream input) throws java.io.IOException, java.io.UnsupportedEncodingException {
        this.stripMultipleNewlines = stripMultipleNewlines;
        this.separator = separator;
        this.fields = new ArrayList < String > ();
        this.in = new BufferedReader(stripBom(input));
    public boolean hasNext() throws java.io.IOException {
        if (eofSeen) return false;
        eofSeen = split( in , fields);
        if (eofSeen) return !fields.isEmpty();
    public List < String > next() {
    static private boolean discardLinefeed(Reader in , boolean stripMultiple) throws java.io.IOException {
        if (stripMultiple) { in .mark(NUMMARK);
                if (c != CRETURN && c != LFEED) { in .reset();
                } else { in .mark(NUMMARK);
        } else { in .mark(NUMMARK);
            if (value == -1) return true;
            else if ((char) value != LFEED) in .reset();
    private boolean skipComment(Reader in ) throws java.io.IOException {
        while ((value = in .read()) != -1) {
                return discardLinefeed( in , stripMultipleNewlines);
    private boolean split(Reader in , ArrayList < String > fields) throws java.io.IOException {
        StringBuilder sbuf = new StringBuilder();
        while ((value = in .read()) != -1) {
                        fields.add(sbuf.toString());
                        sbuf.delete(0, sbuf.length());
                    return discardLinefeed( in , stripMultipleNewlines);
                        fields.add(sbuf.toString());
                        sbuf.delete(0, sbuf.length());
                    if (stripMultipleNewlines)
                        return discardLinefeed( in , stripMultipleNewlines);
                        while ((value = in .read()) != -1) {
                                if ((value = in .read()) == -1) {
                                        fields.add(sbuf.toString());
                                        sbuf.delete(0, sbuf.length());
                                } else if ((c = (char) value) == DQUOTE) {
                                } else if (c == CRETURN) {
                                        fields.add(sbuf.toString());
                                        sbuf.delete(0, sbuf.length());
                                    return discardLinefeed( in ,
                                        fields.add(sbuf.toString());
                                        sbuf.delete(0, sbuf.length());
                                    if (stripMultipleNewlines)
                                        return discardLinefeed( in , stripMultipleNewlines);
                                fields.add(sbuf.toString());
                                sbuf.delete(0, sbuf.length());
                        fields.add(sbuf.toString());
                        sbuf.delete(0, sbuf.length());
                        if (c == COMMENT && fields.isEmpty() &&
                            sbuf.toString().trim().isEmpty()) {
                            boolean eof = skipComment( in );
                            else sbuf.delete(0, sbuf.length());
            fields.add(sbuf.toString());
            sbuf.delete(0, sbuf.length());

java

via DZone.com Feed https://dzone.com

May 22, 2017 at 09:09PM

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s