Tuesday, May 08, 2007

A Parsing CSV File Routine

If any of you already know, parsing a CSV file is a pain in the !@#*! However, some people love to export their data via this format. So, I had a little time on my hands and wanted to share this bit of code with you. It is by no means "clean" and in pure OO format, but it gives you an idea of the pain of parsing this type of file/format.
//**************************************
// Name: A Parsing CSV File Routine
// Description:If any of you already know, parsing a CSV file is a pain in the !@#*! However, some people love to export their data via this format. So, I had a little time on my hands and wanted to share this bit of code with you. It is by no means "clean" and in pure OO format, but it gives you an idea of the pain of parsing this type of file/format.
// By: Steven Jacobs
//
//
// Inputs:None
//
// Returns:None
//
//Assumes:None
//
//Side Effects:None
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.4666/lngWId.2/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

/*
Remember, this is not "clean" and I did 
//     not write the other 
functions (such as get number of lines in the file or # of columns).
All this does is parse out a CSV file in this format:
,,...,",,...",,,...,","
Have fun....
Author: Steven Jacobs
Date: 02/12/2005
PS: I did not use StringTokenizer or the split function because the format of the file did not adhere the return values to their proper strings (due to the delimiters --> the comma).
*/
import java.io.*;

public class parseCSV {

public static void main(String args[]) throws IOException {
    String thisLine;
    String[] fullText = new String[100]; //number of possible lines in your file
    int counter = 0;
    String fName = "";
    FileInputStream fis = new FileInputStream(fName);
    DataInputStream myInput = new DataInputStream(fis);

    while ((thisLine = myInput.readLine()) != null) {
        System.out.println("---------------------------------\nBegin Parsing");

        int sPos = 0;
        int ePos = 1;
        int tDelim = 0;
        String d = "";
        int colCount = 0;
        int collCount = 0;
        boolean fMe = false;
        boolean finalCol = false;

        while ( ePos < thisLine.length()) {
            sPos = thisLine.indexOf(",", ePos);
            tDelim = thisLine.indexOf(",\"", ePos);      

            if (tDelim == ePos) {
                fMe = true;
                collCount = ePos + 1;
                while (collCount <= thisLine.indexOf("\",")) {
                    collCount += 1;
                }
                colCount += 1;
                sPos = collCount;
            }
        
            if (colCount == 47) { //Get Max count of columns
                finalCol = true;
                collCount = ePos + 1;
                while (collCount <= thisLine.lastIndexOf("\"")) {
                    collCount += 1;
                }
                colCount += 1;
                sPos = collCount;
            }
            if (sPos == -1) {
                d = thisLine.substring(ePos,thisLine.length()); 
                System.out.println(d);
                break;
            }else if(ePos == 1) {
                d = thisLine.substring(ePos-1,sPos);
                System.out.println(d);
            }else{
                if (fMe) {
                    d = thisLine.substring(ePos + 1, sPos);
                    fMe = false;
                }else if (finalCol) {
                    d = thisLine.substring(ePos, sPos);
                    finalCol = false;


                }else{
                    d = thisLine.substring(ePos, sPos);
                }
                System.out.println(d);
            }
            
            colCount += 1;
            ePos = sPos + 1;
        }
        counter++;
        fullText[counter] = thisLine;
        System.out.println("\nEnd Parsing\n---------------------------------\n\n");
    }
}
}

0 comments:

 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory