Monday, February 7, 2011

File Parser

package com.mycomp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Set;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
public class FileScanner {
    public static void main (String[] args) throws Exception
    {
        //File in = new File("E:/GTRADE-MANOJ/Demos/data.txt");
        String delimiter=" ";
        String enddelimiter= " ";
        StringTokenizer strTokenizer = null;
     BufferedReader bufferedreader = null;
     int numOfTokens = 0;
  File inputFile = new File("E:\\hshah\\FixData1.log");
  //File inputFile = new File("E:\\GTRADE-MANOJ\\Demos\\C20080408\\C20080408.log");
  String strLine;
  long starttime  = System.currentTimeMillis();
  HashMap headerName1 = new HashMap();
  HashMap valueName1 = new HashMap();
  TreeMap headerName= new TreeMap();
  TreeMap valueName = new TreeMap();
  File tempfile = null;
  String values = "";
  String key = "";
  BufferedWriter out = null;
  System.out.println("Please wait... We are processing your request.");
  try {
   bufferedreader = new BufferedReader(new FileReader(inputFile));
 
          // Create temporary file.
                        File directory = new File("E:\\hshah");
          tempfile = File.createTempFile("tempData", ".ext",directory);
          // Delete temp file when program exits.
        //  tempfile.deleteOnExit();
          out = new BufferedWriter(new FileWriter(tempfile));
      
    while(true){
      if((strLine = bufferedreader.readLine()) == null){ break; }
      int startindex = 0, lastindex = 0;
      if((startindex = strLine.indexOf("8=FIX")) != - 1&& 
      (lastindex = strLine.lastIndexOf("]")) != -1){
       String parseString = strLine.substring(startindex, lastindex);
       strTokenizer = new StringTokenizer(parseString,delimiter); 
       numOfTokens = strTokenizer.countTokens(); 
       values = ""; 
       key = "";
     
       for (int number = 0 ; number < numOfTokens ; number++){ 
       String strData = strTokenizer.nextToken();
       //System.out.println(strData);
       if(strData.indexOf("=") != - 1){
         key = strData.substring(0, strData.indexOf("="));
         String value = strData.substring(strData.indexOf("=") + 1);
         if (!headerName1.containsKey(key)) {
          headerName1.put(key,key);
          //System.out.println(key );
        //  System.out.println(value);
          
        }
         values = key + "=" + value;
       //  System.out.println(values);
         valueName1.put(key, values);
         //System.out.println("4");
                                                                // System.out.println ( values );
                                                                
                                                         } 
       }
       //Write to temp file
      //System.out.println("value " + values);
                                                // System.out.println(headerName.keySet());
                 // System.out.println("HeaderName:"+headerName);
                  valueName= new TreeMap(valueName1);
            Collection a1 = valueName.values();
            Set a = new TreeSet(a1);
            for(Iterator it=a.iterator(); it.hasNext();)
          {
          String element =(String)it.next();
          out.write(element + "$");
          }
            out.write("\n");
      }
   
                                       //  System.out.println(headerName);
                                }
    //System.out.println("headerName Size : " + headerName.size());
    /*Enumeration enumeration =  headerName.keys();
    while (enumeration.hasMoreElements()) {
     String element = (String) enumeration.nextElement();
     System.out.println("element " + element);
    }*/
   
     headerName = new TreeMap(headerName1);
    DataBaseConnection dbconnection = new DataBaseConnection("FixData1");
    dbconnection.createFixedMessageTable(headerName);
    /*
    String[] keys = (String[]) headerName.keySet().toArray(new String[0]);    
    Arrays.sort(keys);
    for (int i = 0; i < keys.length; i++) {
     System.out.println("key  : " + keys[i]);   
    }
    */
/*    TreeMap treeMap = new TreeMap(headerName); 
    Iterator itr = treeMap.keySet().iterator(); 
       while (itr.hasNext()) { 
             String keyValue = (String)itr.next();
             System.out.println("value is="+ keyValue ); 
       } 
*/
    bufferedreader.close();
    out.close();
    bufferedreader = new BufferedReader(new FileReader(tempfile));
    dbconnection.insertFIXValuesIntoTable(bufferedreader,headerName);
  
    long endtime  = System.currentTimeMillis();
    //System.out.println("Your request successfully completed…. Thank you.");
    System.out.println("Time Required for Processing is : " + ((endtime - starttime)/ 1000)/60 + " minutes");
   }catch (IOException ioexception) {
   ioexception.printStackTrace();
  }finally{
   if (bufferedreader != null) {
    bufferedreader.close();
   }
  }
    }
}

No comments:

Post a Comment