CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Feb 2009
    Posts
    32

    Finding Duplicate Files

    Is there an easier way to find duplicate files?

    Code:
    import java.io.*;
    public class Test {
    	public static void main(String[] args) throws Throwable {
    		System.out.print(duplicate(new File("C:\\Users\\Me\\Desktop\\4-01 Living the Dream.avi"),
    			new File("C:\\Users\\Me\\Desktop\\4-01 Living the Dream - Copy.avi")));
    	}
    	public static boolean duplicate(File f1, File f2) throws Throwable {
    		FileInputStream fis1 = new FileInputStream(f1),
    			fis2 = new FileInputStream(f2);
    		byte[] ba1 = new byte[fis1.available()],
    			ba2 = new byte[fis2.available()];
    		
    		if (ba1.length == ba2.length) {
    			fis1.read(ba1);
    			fis2.read(ba2);
    			for (int i = 0; i < ba1.length; i++) {
    				if (ba1[i] != ba2[i]) return false;
    			}
    			return true;
    		}
    		return false;
    		
    	}
    }
    Is this seriously the quickest way? I notice some redundancy when you read the FileInputStream into the byte array, and then check each byte. Is there a way to read the file byte-by-byte and check it on-the-fly? It would replace the byte array, to just one byte variable.
    Last edited by Nim; December 31st, 2009 at 09:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured