Parsing and validating a csv file

Step 1: Provide file location, no of comma separated laves per row and delimiter as  parameter for this method.

Step 2: It will validate the csv file and return Boolean value, If it has same no of comma separated in all the rows.


Public static boolean parseCSVFile(String fileLocation, int columnCount,String deLimiter) throws Exception {

try {

BufferedReader br = new BufferedReader(new FileReader(fileLocation));
 String strLine = "";
 StringTokenizer st = null;
 int lineNumber = 0, tokenNumber = 0;
 while ((strLine = br.readLine()) != null) {
 lineNumber++;
 st = new StringTokenizer(strLine, deLimiter);
 while (st.hasMoreTokens()) {
 tokenNumber++;
 st.nextToken();
 }
 if(tokenNumber > columnCount || tokenNumber < columnCount)
 return false;

tokenNumber = 0;
 }

} catch (Exception e) {
 throw new Exception(e);
 }
 return true;
 }

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s