Link to home
Create AccountLog in
Avatar of mkhan900
mkhan900

asked on

Java program to read 2 files and create a new 3rd file

I am trying to read in 2 files and create a 3rd file which does not have the lines matching from the first file.
Ex: F1 A B 1 C D E F and F2 1 2 3 4 5 6 A B C D E F
Now the out put should be 2 3 4 5 6 since these values are not in F1 file
This is what I have so far, but my logic in FOR LOOP is completely messed. PLZ HELP

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]));
      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();  System.out.println(strg[len]); len++;}
           // System.out.println(lineRead);
      try
       { String wt = new String();
        File f = new File(args[1]);
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line=new String();
        PrintStream out = new PrintStream(new FileOutputStream("result.txt"));
       
      while( (line=br.readLine())!=null )
       {
              
      for (int y =0; y < strg.length; y++)
      {      
          if(line.trim().equals(strg[y]))
             { }
          else
              {
               out.println(line); wt=line;}
               if (wt != null){}
             if (wt.equals(line)) {wt=null;}       
               
           { }
         
       }
       }
       out.close();
       }catch(Exception e){e.printStackTrace();}
       
       
      }
}
       



ASKER CERTIFIED SOLUTION
Avatar of spoxox
spoxox
Flag of Canada image

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