skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, October 17, 2012

Parsing a csv file containing data to be saved into database

Posted by Raju Gupta at 3:00 AM – 0 comments
 
The code helps in parsing data prsent in a comma separated value format in a file to be uploaded in the database. It checks whether the first token of each line in the csv file is empty or not.

public static void parsingData(String uploadFile, FileValueBinder binder) throws FileFormatException {
  int lineNumber = 1;
 String DELIM_STR = "\r\n";
        StringTokenizer lineTokenizer = new StringTokenizer(uploadFile, DELIM_STR);

  line: 
        while (lineTokenizer.hasMoreTokens()) {

            String tokenVal = lineTokenizer.nextToken();
                   
            // tokenize as CSV         
            String nextToken = null;

            String prevToken = "";

            int counter = 0;

            int parseErrorLocation = 0;

            ArrayList errors = new ArrayList();

            ArrayList values = new ArrayList();

            int indexValue=0;

            // returns the index value of the first comma character in the csv file
            indexValue=tokenVal.indexOf(",");

            // check if the comma char is at 0th position 
            if(indexValue==0){
            
             try {
                   throw new FileFormatException("");
                } catch (Exception ex) {
                    throw new FileFormatException("Please enter a valid value at line number: "+lineNumber+ex.getMessage());
                }
            }

           SampleTokenizer commaTokenizer = new SampleTokenizer(tokenVal, DELIM_CHAR);
           
            while (commaTokenizer.hasMoreTokens()) {
                nextToken = commaTokenizer.nextToken();
                values.add(nextToken);
            }

            //pad the values if required.
            if (counter == (binder.getExpectedNumberOfValues() - 1)) {
                values.add(null);
            }

            Iterator valuesIt = values.iterator();
            counter = 0;

            while (valuesIt.hasNext()) {
                String nextValue = (String) valuesIt.next();
                try {
                    binder.addNextToken(nextValue);
                } catch (ParseException e) {
                    errors.add(e);
                    parseErrorLocation = counter;
                    err = true;
                }

                counter++;
            }
           
            
            

            lineNumber++;
        }

        try {
            binder.eof();
        } catch (ParseException e) {
            try {
               //Exception
            } catch (Exception ex) {
                throw new FileFormatException(ex.getMessage());
            }
        }
   }

  }


The SampleTokenizer refers to the following:

 public SampleTokenizer(String str,String delim)
  {
   
    super(str,delim);
    this.string = str;
    this.delim= delim;
    isCountTokensCalled = false;
    tokenList = new ArrayList();

  }
  


 public SampleTokenizer(String str,String delim, String escape)
  {
   
    super(str,delim);
    this.string = str;
    this.delim= delim;
    isCountTokensCalled = false;
    tokenList = new ArrayList();
    this.ESCAPE_STR = escape;
  }

  public int countTokens() {
    isCountTokensCalled = true;
    String value="";

    while (cursorPos < string.length()) {
      if (String.valueOf(string.charAt(cursorPos)).equals(this.ESCAPE_STR) && !isEscape) {
        isEscape = true; 
        escapeStart = currentPos;
      } else if (String.valueOf(string.charAt(cursorPos)).equals(this.ESCAPE_STR) && isEscape) {
        isEscape = false;
        escapeEnd = currentPos;
      } else if (!isEscape && (String.valueOf(string.charAt(cursorPos)).equals(this.delim))) {
        currentPos = cursorPos;
        value = string.substring(previousPos,currentPos);
        if ("".equals(value))
          value = null;
        tokenList.add(count,value);
        count++;
        previousPos = currentPos+this.delim.length();
      } 
      if (cursorPos == string.length()-1 && !(String.valueOf(string.charAt(cursorPos)).equals(this.delim))) {

        value = string.substring(previousPos);
        if (value.equals(""))
          value = null;
        tokenList.add(count,value);
      }
      cursorPos++;      
    } 

    
    if (string.endsWith(",")){
      
      tokenList.add(count,null);
      count++;
    } else count++;
    
    return count;    
  }
  
  public boolean hasMoreTokens()  {

      if (!isCountTokensCalled)
        this.countTokens();
      if (count > tokenPos)
        return true;
      return false;
  }

  public String nextToken() {
    String token = (String)tokenList.get(tokenPos);
    tokenPos++;
    return token;
    
  }


Labels: CSV Example , Java Database Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger