Results 1 to 7 of 7
  1. #1
    bbeng2 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Help with a program

    Hello everyone. I'm new here and I am trying to write this program and am completely stuck. What i want the program to do is to read in two directory paths from the user and compare those directories and print out the differences. The catch is i only want it to compare 2 directories deep. For example, to compare the music folder on my hard drive and on my external hard drive. My folders are organized: my music>artist>albums, so i just want it to be able to search these folders, not the files inside the albums folders, to show what's different. I hope that makes sense... Anyways, here is the class that I have written to do the comparison.

    Any suggestion on how to make this work/why it doesn't work or any suggestion on a more efficient way to go about doing it would be greatly appreciated. Thank you.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class NewFunctionClass {
    	private static File[] mainDir1;
    	private static File[] mainDir2;
    	private static File[] subDir1;
    	private static File[] subDir2;
    
    	public static void readDir1(String dirName1) {
    
    		File dir1 = new File(dirName1);
    
    		mainDir1 = dir1.listFiles();
    
    	}// end readDir1
    
    	public static void readDir2(String dirName2) {
    
    		File dir2 = new File(dirName2);
    
    		mainDir2 = dir2.listFiles();
    
    	}// end readDir1
    
    	public static void compareDirs(String dir1, String dir2) {
    
    		int i,j;
    		List<File> mainDir1List;
    		List<File> subDir1List;
    		List<File> mainDir2List;
    		List<File> subDir2List;
    
    		ToArrayList a = new ToArrayList();
    
    		// compare first directory to second
    
    		for (i = 0; i < mainDir1.length; i++) {  //<<<<ERROR HERE>>>>>
    			if (mainDir1[i].isDirectory()) {
    				subDir1 = mainDir1[i].listFiles();
    			}// end if
    
    			mainDir1List = a.ArrayToArrayList(mainDir1);
    			subDir1List = a.ArrayToArrayList(subDir1);
    
    			for (j = 0; j < mainDir2.length; j++) {
    				if (!mainDir1List.contains(mainDir2[j])) {
    					System.out.println("Didn't find " + mainDir2[j].toString()
    							+ " in " + dir1);
    				}// end if
    
    				if (!subDir1List.contains(mainDir2[j])) {
    					System.out.println("Didn't find " + mainDir2[j].toString()
    							+ " in " + dir1);
    				}// end if
    			}// end for
    		}// end for
    		
    		// now compare second directory to first
    
    		for (i = 0; i < mainDir2.length; i++) {
    			if (mainDir2[i].isDirectory()) {
    				subDir2 = mainDir2[i].listFiles();
    			}
    
    			mainDir2List = a.ArrayToArrayList(mainDir2);
    			subDir2List = a.ArrayToArrayList(subDir2);
    
    			for (j = 0; j < mainDir1.length; j++) {
    				if (!mainDir2List.contains(mainDir1[j])) {
    					System.out.println("Didn't find " + mainDir1[j].toString()
    							+ " in " + dir2);
    				}
    
    				if (!subDir2List.contains(subDir1[j])) {
    					System.out.println("Didn't find " + mainDir1[j].toString()
    							+ " in " + dir2);
    				}
    			}//end for
    		}//end for
    	}// end method
    
    }// end class
    Also here is the code for the class that i used to put the arrays in an ArrayList:

    Java Code:
    import java.util.*;
    import java.io.*;
    public class ToArrayList {
    	
    	List<File>myArrayList;
    	
    	public ToArrayList(){
    		
    		myArrayList = new ArrayList<File>();
    	}
    	public List<File> ArrayToArrayList(File[] myArray)
    	{
    		int i;
    		
    		for(i = 0; i < myArray.length; i++)
    		{
    			myArrayList.add(myArray[i]);
    		}
    		
    		return myArrayList;
    	}
    
    }
    Last edited by bbeng2; 04-17-2009 at 02:34 AM.

  2. #2
    bta's Avatar
    bta is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    does anyone know.i am interested too

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Look at the File api. I think there's some documentation on only going a certain level deep or to not check subFolders. I'm sorta interested in this too. Would help me get all my music off my external onto my internal
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    bbeng2 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Haha, yeah if anyone could help at all. At this point i'm just trying to get it to show what is different but at some point i would like to be able to add a function to actually copy the files over that are missing.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    mm the most helpful thing I could see was listSubFiles or listFiles. Try using a delimiter to take the entire just the first 3 or 4 "\"
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    bbeng2 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Yeah, i've looked through the api too. So are you saying I should use like a string buffer and make a substring to the 3rd or 4th "\" and use that as another directory to load into an array? Am I following you right?

  7. #7
    bbeng2 is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    Just to clarify, my program compiles fine but when I run it I get a NullPointerException error. I edited the code to show where it says the error is.

    Also, if it would help I can upload my class containing the main method and also my inputClass.

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

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