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
C:\test
C:\test\lev1
C:\test\lev1\lev2
C:\test\lev1\lev2\lev3
C:\test2
C:\test2\lev1
C:\test2\lev1\lev2
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
that??
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;
if(args.length==0){
System.err.println("Please specify a directory name");
return;
}
entry = new File(args[0]); //user specified
entry2 = new File(args[1]); //user specified
listDirectory(entry,entry2);
}
public static void listDirectory(File entry, File entry2) throws IOException{
try{
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);
if(!entry.exists()){
System.out.println(entry.getName() + " not found.");
return;
}
if(entry.isDirectory()){
String[] fileName = entry.list();
String[] fileName2 = entry2.list();
List list1 = Arrays.asList(fileName);
List list2 = Arrays.asList(fileName2);
Iterator iter = list1.iterator();
while(iter.hasNext()){
String item=(String)iter.next();
if (list2.contains(item)){
File item1 = new File(item);
if(item1.isDirectory()){
System.out.println(item1.getName() + " is in both lists");
printWriterNoError.println(" is in both lists");
printWriterNoError.close();
}
}
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");
}
}
}
if(entry.isDirectory()){
//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);}
}
}