Tuesday, October 12, 2010

Read CSV File Using Regular Expressions in Java

Example 2:

Reading CSV File and Storing it in Database with Regular Expressions

Step 1:

File CsvFile =new File("File Path");
FileReader inpFile = new FileReader(CsvFile);
BufferedReader inpReader = new BufferedReader(inpFile);

String inpLine;
Step 2:

//Reading Line By Line
while ((inpLine = inpReader.readLine()) != null)
{
  List result = parseCSV(inpLine, ",");
  String Fields[] = (String[]) result.toArray(new String[0]);
  DoInsertIntoDB(Feilds);
}
Step 3:

//Using Regular Expression
public List parseCSV(String csv, String delim)
{
  final Pattern NEXT_COLUMN = nextColumnRegex(delim);
  final List strings = new ArrayList();
  final Matcher matcher = NEXT_COLUMN.matcher(csv);
  while (!matcher.hitEnd() && matcher.find()) 
  {
    String match = matcher.group(1);
    if (match.matches(quoted)) 
    {
      match = match.substring(1, match.length() - 1);
    }
    match = match.replaceAll("\"\"", "\"");
    strings.add(match);
  }
return strings;
}
private Pattern nextColumnRegex(String comma) 
{
  String unquoted = "(:?[^\"" + comma + "]|\"\")*";
  String ending = "(:?" + comma + "|$)";
return Pattern.compile('(' + quoted + '|' + unquoted + ')'
                                               + ending);
}
Step 4:

//Inserting Into Database
public void DoInsertIntoDB(String[] inpFeilds)
{
  String Query="insert into emp(id,name)values(?,?)";
  PreparedStatement InsST=connetion.prepareStatement(Query);
  for (int i = 0; i < inpFeilds.length; i++)
  {
    inpFeilds[i] = inpFeilds[i].replace("\"", "");
  }

  InsST.setString(1, inpFeilds[0]);
  InsST.setString(1, inpFeilds[2]);
  InsST.executeUpdate();
}

//This is what you need to do when 
                 reading .csv file.

1 comment: