Important alert: (current site time 7/15/2013 3:11:04 PM EDT)
 

VB icon

A Parsing CSV File Routine

Email
Submitted on: 2/12/2005 3:10:32 AM
By: Steven Jacobs 
Level: Advanced
User Rating: By 3 Users
Compatibility: Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 41376
author picture
(About the author)
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// 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
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4666&lngWId=2//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:
<text>,<text>,...,"<text>,<text>,...",<text>,<text>,...,"<text>,<text>"
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 = "<your *.csv file>";
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");
}
}
 
}


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/7/2005 1:31:59 PM

fantastic code
(If this comment was disrespectful, please report it.)

 
1/11/2007 8:52:49 AMsandeep patil

thank for code help!
(If this comment was disrespectful, please report it.)

 
2/18/2008 9:25:00 AMRajasekhar

Hello Steven Jacobs,
Good job.. keep it up.

Regards
Rajasekhar
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.