Compare Strings from between 2 files using Java -


public class comparee {  static int count=0; public static void main(string[] args) throws ioexception {      string a,b;      filereader fi = new filereader(new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\a.txt"));  // new file      filereader fii = new filereader(new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\b.txt"));  // new file      bufferedreader br =new bufferedreader(fi); //new      bufferedreader br1 =new bufferedreader(fii); //old     filewriter fw = new filewriter(new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\samnew.txt"));      int count = 0;     while((a=br.readline()) != null)     {          while((b=br1.readline()) != null)         {              if(!(a.equals(b)))              {                 count++;                 fw.write(a);             }          }         system.out.println(count);      }   } } 

hi, trying compare string a.txt , b.txt reading line line. write line on samdata.txt not available in a.txt available on b.txt. appreciate :) thanks

p.s above code logic incorrect

comparing files complex operation, because have ahead in both files in order find next matching lines.

now, if absolutely(!) sure b.txt contains lines a.txt, in same order a.txt, has lines inserted @ various places, following might ok.

btw: variable names confusing, i'm renaming them , using try-with-resources ensure readers , writer closed.

file filea = new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\a.txt"); file fileb = new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\b.txt"); file filenew = new file("c:\\users\\ibm_admin\\desktop\\sam_public_monthly_20150802\\samnew.txt"); int count = 0; try (reader readera = new bufferedreader(new filereader(filea));      reader readerb = new bufferedreader(new filereader(fileb));      printwriter writer = new printwriter(new filewriter(filenew))) {     // read first line of each file     string linea = readera.readline();     string lineb = readerb.readline();     // compare lines     while (linea != null || lineb != null) {         if (lineb == null) {             // oops! found line in file             linea = readera.readline(); // read next line file         } else if (linea == null || ! linea.equals(lineb)) {             // found new line in file b             writer.println(lineb);             lineb = readerb.readline(); // read next line file             count++;         } else {             // lines equal, read next line both files             linea = readera.readline();             lineb = readerb.readline();         }     } } system.out.println(count); 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

Python Error - TypeError: input expected at most 1 arguments, got 3 -

dns - How To Use Custom Nameserver On Free Cloudflare? -