Link to home
Create AccountLog in
Avatar of mkhan900
mkhan900

asked on

Java Program to Read in 2 files and compare

Need some expert help with a program which should accept 2 files and create a 3rd file after comparing the lines in first and second file. Ex:
File1      File2      File3
1173      6356      1173
3757      6118      3757
4029      8404      4029
6356      6940      7661
6118      8834      798
8404      9063      856
6940      9184      4714
8834      9684      2425
9063            6187
9184            
9684            
7661            
798            
856            
4714            
2425      
6187      
 Thanks
Avatar of cmalakar
cmalakar
Flag of India image

If you are facing any problem in the above accepted solution, please let us know..

We will help you..
Avatar of mkhan900
mkhan900

ASKER

Thanks for the response.
I am struck with the logic in the loop. The 3rd file(output of the prog) has duplicate values, since its looping around with a single value from the second file.
 
Can you help me with the code in the FOR LOOP

import java.io.*;
import java.util.*;
import java.text.*;

public class del_matching_lines
{
      public static void main(String args[]) throws Exception
      {


      DataInputStream inputFile = new DataInputStream(new FileInputStream(args[0]));
      PrintStream Out = new PrintStream(new FileOutputStream("result.txt"));
      String lineRead = new String();
      Vector string = new Vector(); int l=0;
      while ( ( lineRead = inputFile.readLine() ) != null )
            { string.addElement(lineRead);  l++;
              }
           
            String[] strg = new String[l];
            Vector sstrg = new Vector();
            Enumeration enumer = string.elements (); int len=0;
            while (enumer.hasMoreElements ())
            {  strg[len]=enumer.nextElement().toString(); len++;}


           DataInputStream readFile = new DataInputStream(new FileInputStream(args[1]));
           String lRead = new String();
           Vector stg = new Vector(); int M=0;
             

          while ( ( lRead = readFile.readLine() ) != null )
            { stg.addElement(lRead);  M++; }


                String[] strg1 = new String[M];
            Vector sstrg1 = new Vector();
            Enumeration enumer1 = stg.elements (); int len1=0;
            while (enumer1.hasMoreElements ())
            {  strg1[len1]=enumer1.nextElement().toString(); len1++;}


          for ( int k = 0; k < strg1.length ; k++ )
           {  
                 System.out.println(strg1[k]);
                 
              for ( int j = 0; j < strg.length ; j++ )

                    { System.out.println(strg[j]);}          
         
           }                
         
                          
         

      }
}
       
ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account