This discussion is archived
2 Replies Latest reply: Dec 31, 2007 1:31 AM by 800308 RSS

Reading, comparing and appending two files is taking lot of time

807601 Newbie
Currently Being Moderated
public static void main(String args[])
{
     BufferedReader bSource = null;
     String sSource="";
     String sSourceData="";
     
     BufferedReader bDestination = null;
     String sDestination="";
     String sDestinationData="";
     try
     {
          bSource = new BufferedReader(new FileReader("C:/TEMP/Source.map"));
          while((sSource = bSource.readLine()) != null)
          {
               sSourceData+=sSource;
          }
          bDestination = new BufferedReader(new FileReader("D:/TEMP/Destination.map"));
          while((sDestination = bDestination.readLine()) != null)
          {
               sDestinationData+=sDestination;
          }
     }
     catch(IOException ioe)
     {
          System.out.println(ioe.getMessage());
     }
     finally
     {
          try 
          { 
               bSource.close();
               bDestination.close();
          }
          catch(Exception e) 
          {
          }
     }
   
     if(sDestinationData.contains(sSourceData))
     {
          fDestinationFlag=false;
     }
     else
     {
          fDestinationFlag=true;
     }
     if(fDestinationFlag)
     {
          BufferedReader br2 = null;
          BufferedWriter bw2 = null;
          try
          {
               br2 = new BufferedReader(new FileReader("C:/TEMP/Source.map"));     
               bw2 = new BufferedWriter(new FileWriter("D:/TEMP/Destination.map",true));
               while((line2 = br2.readLine()) != null)
               { 
                    bw2.write(line2); 
                    bw2.newLine();
                    bw2.flush();  
               }
          }
          catch(IOException ioe)
          {
               System.out.println(ioe.getMessage());
          }
          finally
          {
               try 
               { 
                    br2.close(); 
               }
               catch(Exception e) 
               {
               }
               try 
               { 
                    bw2.close(); 
               }
               catch(Exception e) 
               {
               }
          }
     }   
}
I am comparing the contents of the two files Source.map (containing 3 lines) and Destination.map (containing 5000+ lines). The contents of the Source.map gets appended in the Destination.map only if it does not contain the same.
Now my program is doing the necessary, but it is taking lot of time. It's taking 1 min 26 seconds.
Could anyone please tell me what do I modify in may program to make it execute much faster.