Wednesday, November 12, 2014

Read file from file system, Count number of words present in this file, and Sort by frequency of words in the file.



// Base class of Exception handling template


public abstract class InputStreamProcessingTemplate 
{
 
 public void process(String fileName) throws MyException 
 {
  InputStream input = null;
  DataInputStream dis = null;
  BufferedReader br = null;
  IOException processException = null;
  
  try{
   input = new FileInputStream(fileName);
   dis = new DataInputStream(input);
   br = new BufferedReader(new InputStreamReader(dis));
   doProcess(br);
   
  }catch(IOException e) {
   processException = e;
  }finally {
   if(input != null)
   {
    try {
     br.close();
    } catch (IOException e) {
     if(processException != null)
     {
      throw new MyException(processException, e,"Error message..." +fileName);
     }else{
      throw new MyException(e, "Error closing InputStream for file " +fileName);
     }
    }
   }
   if(processException != null)
   {
    throw new MyException(processException, "Error closing InputStream for file " +fileName);
   }
  }
 }
 
 public abstract void doProcess(BufferedReader input) throws IOException;
}




// Read file from file system and count no. of words by using Exception handling 
// template.
//

 public Map getWordCount(String fileName) throws MyException
 {
  final Map myMap = new HashMap();
  
  new InputStreamProcessingTemplate() {
   
   @Override
   public void doProcess(BufferedReader input) throws IOException 
   {
    String sb = null;
    
    while((sb = input.readLine()) != null)
    {
     StringTokenizer st = new StringTokenizer(sb, " ");
     
     while(st.hasMoreTokens())
     {
      String tmp = st.nextToken().toLowerCase();
      
      if(myMap.containsKey(tmp))
       myMap.put(tmp, myMap.get(tmp)+1);
      else
       myMap.put(tmp, 1);
     }
     
    }
   }
  }.process(fileName);
  
  return myMap;
 }
 


// Sort by frequency of words in HashMap in asc order
//

 public List< Entry > sortByValue(Map myMap)
 {
  
  Set< Entry > set = myMap.entrySet();
  
  List< Entry > list = new ArrayList< Map.Entry >(set);
  
  Collections.sort(list, new Comparator< Map.Entry >() {

   @Override
   public int compare(Map.Entry o1, Map.Entry o2) {
    
    return o1.getValue().compareTo(o2.getValue());
   }
   
  });
  
  
  return list;
  
 }

No comments:

Post a Comment