-5

I am trying to print name of files from two folders, and this code compiles but not giving anything on running it.

The main target here is to find common name files in two folders, I have stored file names in two arrays and then i will applying sorting and will find common files.

 package javaapplication13;

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

    public class ListFiles1 

    {

        public static void main(String[] args) 
        {

            String path1 = "C:/"; 
            String path2 = "D:/"; 

            File folder1 = new File(path1);
            File folder2 = new File(path2);

            String[] f1=folder1.list();

            File[] listOfFiles1 = folder1.listFiles(); 
            File[] listOfFiles2 = folder2.listFiles(); 

            ArrayList<String> fileNames1 = new ArrayList<>();
            ArrayList<String> fileNames2 = new ArrayList<>();

            for (int i = 0; i < listOfFiles1.length; i++) 
            {

                if (listOfFiles1[i].isFile()) 
                {
                    fileNames1.add(listOfFiles1[i].getName());//wow
                    System.out.println(listOfFiles1[i].getName());
                }

            }

            for (int i = 0; i < listOfFiles2.length; i++) 
            {

                if (listOfFiles2[i].isFile()) 
                {
                    fileNames2.add(listOfFiles2[i].getName());//seriously wow
                }
            }

        }
    }
Shubham
  • 33
  • 1
  • 10

2 Answers2

0

The main target here is to find common name files in two folders, I have stored file names in two arrays and then i will applying sorting and will find common files.

I don't like the two array thing. Also, a duplicate file should probably have the same length as well as the same name. If you are really going for just names, you can remove the f.length() == temp.length() part of the condition.

private static void findDups(String dirName1, String dirName2){
    File dir1 = new File(dirName1);
    File dir2 = new File(dirName2);
    Map<String,File> fileMap = new HashMap<String,File>();
    File[] files = dir1.listFiles();
    for(File f : files){
        fileMap.put(f.getName(),f);
    }

    files = dir2.listFiles();
    StringBuilder sb = new StringBuilder(100);

    for(File f : files){
        File temp = fileMap.get(f.getName());
        if(temp != null && f.length() == temp.length()){
            sb.append("Found duplicate files:  ")
              .append(temp.getAbsolutePath())
              .append(" and ")
              .append(f.getAbsolutePath());
            System.out.println(sb);
            sb.delete(0,sb.length());
        }
    }
}
MadConan
  • 3,749
  • 1
  • 16
  • 27
0

Loop through both the ArrayLists you have. Each ArrayList contains the file names as it is. You'll need a nested loop (a loop inside a loop). In the core of the nested loops, you want to do a compare between current position of each ArrayList. You can use .equals() method for this. The pseudo code is something like:

//create a new ArrayList called "commonNameList"
// loop through fileNames1 with position variable "i"
    //loop through fileNames2 with position variable "j"
        //tempFileName1  = fileNames1.get(i)
        //tempFileName2 = fileNames2.get(j)
        //if tempFileName1  equals tempFileName2
            //commonNameList.add(tempFileName1)

Check these out:

http://mathbits.com/MathBits/Java/Looping/NestedFor.htm

Simple nested for loop example

How do I compare strings in Java?

Community
  • 1
  • 1