1

Possible Duplicate:
Compare the content of 2 text files in java language

I am trying to compare strings of two .txt files in an android application. can you please tell me how to proceed?? i want to insert the code in this

try {
     URL url = new URL("httpurl");                             
     URLConnection ucon = url.openConnection();
     InputStream is = ucon.getInputStream();
     BufferedInputStream bis = new BufferedInputStream(is);
     ByteArrayBuffer baf = new ByteArrayBuffer(50);
     int current = 0;
     while ((current = bis.read()) != -1) {
           baf.append((byte) current);
     }

     FileOutputStream fos = new FileOutputStream("/mnt/sdcard/random.txt");
     fos.write(baf.toByteArray());
     fos.close();
} catch (IOException e) {
     Log.d("ImageManager", "Error: " + e);
}
Community
  • 1
  • 1
user1437027
  • 57
  • 1
  • 1
  • 10

2 Answers2

4

You shouldn't read whole files into memory and then compare them!

You can read both files blockwise, compare each block pairs and stop reading if blocks are different. Also you should reuse memory buffer for blocks.

This approach gives you early stop (good for performance) and manages memory (so you can compare very big files)

Remember that this is time consuming operation , so you shouldn't do this in UI thread. Use AsyncTask for this.

Also, I'd recommend to compare file sizes before reading files. This is very fast and gives you very early stop in case of files have different sizes (very good for performance)

Arseniy
  • 1,737
  • 1
  • 19
  • 35
  • +1 in my new answer I am try do the same ... is it right ? – Dheeresh Singh Jun 06 '12 at 07:46
  • 1
    +1, reading the complete files at once is overkill. I don't know if it is available on Android but commons-io has a helper class to do this: http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#contentEquals%28java.io.File,%20java.io.File%29 – ftr Jun 06 '12 at 07:53
  • commons-io is available to Android, so it's really good practice to reuse this code – Arseniy Jun 06 '12 at 08:03
  • how do we retrieve the last line from a server?? – user1437027 Jun 23 '12 at 11:29
2
       File dir = Environment.getExternalStorageDirectory();

       File yourFile1 = new File(dir, "path/to/the/file/inside/the/textfile1.txt");
       File yourFile2 = new File(dir, "path/to/the/file/inside/the/textfile2.txt");

       put the check for file exists ..........

       FileInputStream fstream1 = new FileInputStream(yourFile1 );  
       FileInputStream fstream2 = new FileInputStream(yourFile2 );  

     DataInputStream in1 = new DataInputStream(fstream1);  
      BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));  

    DataInputStream in2 = new DataInputStream(fstream2);  
      BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));  

    String strLine1, strLine2;  
     boolean isSame = true;
    while ((strLine1 = br1.readLine()) && strLine2 = br2.readLine()) ) != null)   {  
          if(strLine1.equals(strLine2))  
               System.out.println(strLine1)
          else{                     //optional just try to optimize can remove
                  //not same 
                  isSame = false;
                   break;
                }  
    } 
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • 1
    Looks OK. Also, I'd recommend to use AsyncTask for this operation to avoid blocking of UI thread. – Arseniy Jun 06 '12 at 07:53