Array Duplicate Comparison

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 628 Views - Last Post: 06 February 2012 - 12:14 PM Rate Topic: -----

Topic Sponsor:

#16 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Re: Array Duplicate Comparison

Posted 06 February 2012 - 09:59 AM

CasiOo,

I suspected as much! I solved it by creating a variable 'z' of type File in the first for loop. Then in the second for loop I assigned the record at hist[j] to z so I could use it later! To test that the size checking was working I modified one of the files and got this output:

*Duplicate File: - C:\DIFFCHECK\DIR1\T0000117.075Q - 318 - C:\DIFFCHECK\DIR2\T0000117.075Q - 532
*Duplicate File: - C:\DIFFCHECK\DIR1\T0000122.305Q - 1876 - C:\DIFFCHECK\DIR2\T0000122.305Q - 1876
*Duplicate File: - C:\DIFFCHECK\DIR1\T0000127.305Q - 3106 - C:\DIFFCHECK\DIR2\T0000127.305Q - 3106

Works great! Here's my code, hope the formatting comes out right when I paste it. Those unused imports are because this is my 'working' version for my own simplicity/sanity. The final outputs a log file.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

public class diffcheck2
{
public static void main(String[] args) throws IOException
{
//populate the arrays
	File f = new File("C:/DIFFCHECK/DIR1/");                
	File[] rec = f.listFiles();  
	
	File g = new File("C:/DIFFCHECK/DIR2/");                
	File[] hist = g.listFiles();
	
	for(int i=0;i<rec.length;i++){
	    boolean found = false;    
	    File z = null;
	    
	    	for(int j=0;j<hist.length;j++){       
	    		if((rec[i].getName().equals(hist[j].getName()))){  
	    			found = true;  
	    			z = hist[j];
	    			break;      
	    					}    
	    				}  
	    	
	    		if (found) { 
	    			System.out.println("*Duplicate File: "+ 
					/*rec[i].getName() +*/ " - " +rec[i] + " - " + rec[i].length()+ " - " + z + " - " + z.length()
						 
						);	

	    		}
			}
		} 
	}




Was This Post Helpful? 0
  • +
  • -

#17 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 3755
  • View blog
  • Posts: 9,705
  • Joined: 16-October 07

Re: Array Duplicate Comparison

Posted 06 February 2012 - 11:51 AM

Use some methods. They're free. Really.

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

public class diffcheck2 {
	private boolean isMatch(File f1, File f2) {
		if (!f1.getName().equals(f2.getName())) { return false; }
		return f1.length()==f2.length();
	}
	
	private boolean isMatch(File f1, File[] fl2) {
		for(File f2 : fl2) { if (isMatch(f1, f2)) { return true; } }
		return false;
	}

	private void findDups(File[] fl1, File[] fl2) {
		for(File f1 : fl1) {
			if (isMatch(f1, fl2)) {
				System.out.println("*Duplicate File: " + f1 + " - " + f1.length());
			}
		}
	}
	
	public void findDups(String dir1, String dir2) {
		findDups(new File(dir1).listFiles(), new File(dir2).listFiles());
	}
	
	public static void main(String[] args) throws IOException {
		new diffcheck2().findDups("C:/DIFFCHECK/DIR1/","C:/DIFFCHECK/DIR2/");                
	}
}


Was This Post Helpful? 1
  • +
  • -

#18 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1778
  • View blog
  • Posts: 3,357
  • Joined: 19-March 11

Re: Array Duplicate Comparison

Posted 06 February 2012 - 12:14 PM

Quote

Use some methods. They're free. Really.


This is why I love having baavgai around.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2