skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, October 3, 2012

Adding Columns in two text files

Posted by Admin at 10:20 PM – 0 comments
 
The two text files are read from the disk and Hash Maps are being extensively used to keep the count for the corresponding APIs (keys), Incase the transactions are present in only one region,it prints the count for only one file and skips the addition part. It can also be extended to any number of keys.
Presently its restricted to two columns but can be extended by alterations in code.This has been successfully implemented in our project and could be useful wherever load balancing happens across two regions.
import java.io.*;
import java.util.*;

class addEnv
{
public static void main (String [] args) throws IOException

 {
 FileReader f = new FileReader("c:/addfiles/rv.txt");
 BufferedReader br =new BufferedReader(f);
 String s="";
 HashMap hmrv = new HashMap();
 HashMap hmrvE = new HashMap();
 String key = "";
 String TCI ="";
 String FCI = "";
 int TCIint = 0;
 int FCIint=0;
   //RV file

 while ((s=br.readLine())!=null)
  {
   StringTokenizer st =new StringTokenizer(s,"|");
         while (st.hasMoreTokens())
    {
     key=st.nextToken().trim();
     TCI=st.nextToken().trim();
     FCI=st.nextToken().trim(); 
     TCIint = Integer.parseInt(TCI);
     FCIint = Integer.parseInt(FCI);
     hmrv.put(key, new Integer(TCIint));
     hmrvE.put(key, new Integer(FCIint));
     }
  }
  br.close();
  f.close();

 //RT file 
  FileReader fv = new FileReader("c:/addfiles/rt.txt");
  BufferedReader brv =new BufferedReader(fv);
  s = "";
  key="";
  TCI="";
  FCI="";
  FCIint=0;
  TCIint=0;
  int valRV=0;
  int valRVE=0;
  int totalVal=0;
  int errorVal=0;
 
  while ((s=brv.readLine())!=null)
  {
   
  StringTokenizer st =new StringTokenizer(s,"|");
         while (st.hasMoreTokens())
    {
   key=st.nextToken().trim();
    if (hmrv.get(key)!=null && hmrvE.get(key) != null)
    {

     valRV=((Integer)hmrv.get(key)).intValue();
     TCI = st.nextToken().trim();
     TCIint= Integer.parseInt(TCI);
     totalVal = TCIint + valRV;
     valRVE =((Integer)hmrvE.get(key)).intValue();
     FCI = st.nextToken().trim();
     FCIint= Integer.parseInt(FCI);
     errorVal = FCIint + valRVE;
     hmrv.remove(key);
     hmrvE.remove(key);
     hmrv.put(key, new Integer (totalVal));
     hmrvE.put(key, new Integer (errorVal));
     }
     else
     {
     hmrv.remove(key);
     hmrvE.remove(key);
      TCI = st.nextToken().trim();
      FCI = st.nextToken().trim();
      TCIint= Integer.parseInt(TCI);
      FCIint= Integer.parseInt(FCI);
      hmrv.put(key, new Integer (TCI));
      hmrvE.put(key, new Integer (FCI));
   
     }
    }
    }
   brv.close();
   fv.close();
   //output file
   Set set = hmrv.entrySet();
   Iterator i =set.iterator();
   FileWriter fwTotal = new FileWriter("c:/addfiles/TCICount.txt");
   while(i.hasNext())
    {
     Map.Entry me = (Map.Entry)i.next();
     fwTotal.write(me.getKey()+" : "+me.getValue()+ "\n");
     }
   fwTotal.close();
   //output error file
   Set setError =hmrvE.entrySet();
   Iterator iE =setError.iterator();
   FileWriter fwError = new FileWriter("c:/addfiles/FCICount.txt");
   while(iE.hasNext())
    {
     Map.Entry me = (Map.Entry) iE.next();
     fwError.write(me.getKey()+" : "+me.getValue()+ "\n");
     }
   fwError.close();

   }
}

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