• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing directories - revisited

Waze Mvarume
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi java friends. I am back again. Last week I asked you for ideas on how to
compare directory structures and I would like to thank you for the wonderful suggestions - Jason and Stan. My apologies for posting this as a new topic but I thought I would catch your attention faster this way. Here is what I have done thus far with regards to the comparing two directories problem. I desperately need your assistance again. This program gets 2 arguments at runtime. These arguments are directory paths whose subdirectories will be compared. The idea is to get relative paths and then comapre them. For example at runtime if I enter the arguments C:\test and C:\test2 having
the program should just compare the RELATIVE paths of C:\test and C:\test2. That
means \lev1 and \lev1\lev2 are matches. lev1\lev2\lev3 is not a match. The program
writes to info.txt if there is a match and infoError.txt if there is no match.
The two problems I am running to
1. I am using lists and comparing the items in the lists using contains() method.
For this method to work, I need to know how to retrieve RELATIVE paths. How do I do
2. I can write to the console but when I direct my output to a file, nothing appears
in the file. I have tested my method writing to a text file and it works if I
manually create the arrays in the program.

Please help...........
import java.io.*;
import java.util.*;
public class DirFinal2{
public static void main(String args[]) throws IOException, NumberFormatException{
File entry;
File entry2;
System.err.println("Please specify a directory name");
entry = new File(args[0]); //user specified
entry2 = new File(args[1]); //user specified
public static void listDirectory(File entry, File entry2) throws IOException{
FileOutputStream outputFileError = new FileOutputStream("infoError.txt");
OutputStreamWriter outputStreamError = new OutputStreamWriter(outputFileError,"8859_1");
PrintWriter printWriterError = new PrintWriter(outputStreamError,true);
FileOutputStream outputFileNoError = new FileOutputStream("info.txt");
OutputStreamWriter outputStreamNoError = new OutputStreamWriter(outputFileNoError,"8859_1");
PrintWriter printWriterNoError = new PrintWriter(outputStreamNoError,true);
System.out.println(entry.getName() + " not found.");
String[] fileName = entry.list();
String[] fileName2 = entry2.list();
List list1 = Arrays.asList(fileName);
List list2 = Arrays.asList(fileName2);
Iterator iter = list1.iterator();
String item=(String)iter.next();
if (list2.contains(item)){
File item1 = new File(item);
System.out.println(item1.getName() + " is in both lists");
printWriterNoError.println(" is in both lists");
if (!list2.contains(item)){
File item2 = new File(item);
System.out.println(item2.getPath() + " is NOT in both lists");
printWriterError.println(" is NOT in both lists");
//create list of entries for this directory
String[] fileName = entry.list();
if(fileName==null) return;
for(int i=0;i<fileName.length;i++){
//create file object for entry
File item = new File(entry.getPath(), fileName);
listDirectory(item,entry2);//call with entry2 argument else error
}catch(IOException e){System.out.println("Error: " + e);}
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of issues:
- your logging is a little bit brittle, since you only log on success of a test. Its a good idea to also log if things don't happen as you hope they will.
- File item = new File(entry.getPath(), fileName); I don't think this will compile.
As to your questions:
1. You pass in an absolute path, so from then on everything is relative. Once you have your initial directories, all subdirectories are relative from there. So, substring the last directory from the path and compare.
2. Try calling flush() on your streams.
Hope that helps.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic