显示列表数组中的文件名差异(Java)

时间:2017-07-08 18:45:04

标签: java arrays collectors

我正在尝试比较文件,我的解决方案适用于其中包含少量文件的文件夹,例如10个文件夹,但不包含超过200个文件名的文件夹。我的代码的最后一部分返回NULL或Results = [ ]通常文本文件名称在这里显示两个文件夹之间的差异,任何帮助表示赞赏。问题是我的代码的最后一部分

package nameCompare;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


// https://stackoverflow.com/questions/19662384/comparing-file-names
// http://www.javaprogrammingforums.com/java-programming-tutorials/3-java-program-can-list-all-files-given-directory.html

public class fileNameCompare {

    public static void main(String[] args) {
                             // first directory path here
        String path1 = "C:/Users/Jonathan/Desktop/ADP/MagMediaAfter";
        //String path1 = "C:/Users/Jonathan/Desktop/FolderA";

        // second directory path here
        String path2 = "C:/Users/Jonathan/Desktop/ADP/MagMediaAfter";
        //String path2 = "C:/Users/Jonathan/Desktop/FolderB";


        File folder1 = new File(path1);
        int fileCount = folder1.list().length;

        File folder2 = new File(path2);
        int fileCountTwo = folder2.list().length;


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

        //we'll need these to store the file names as Strings
        ArrayList<String> BeforeMM = new ArrayList<String>();
        ArrayList<String> AfterMM = new ArrayList<String>();

        //get file names from directory
        for (int i = 0; i < listOfFiles1.length; i++)
        {
            if (listOfFiles1[i].isFile())
            {
                BeforeMM.add(listOfFiles1[i].getName());//wow
            }
        }

        for (int i = 0; i < listOfFiles2.length; i++)
        {
            if (listOfFiles2[i].isFile())
            {
                AfterMM.add(listOfFiles2[i].getName());//seriously wow
            }
        }

            // Number of Files in each Folder or Directory
            System.out.println("Files in FolderA / BeforeMM ----> " + fileCount );
            System.out.println("Files in FolderB / AfterMM -----> " + fileCountTwo );
            System.out.println("------------------------------------");

            System.out.println( "Size/Number of Array Elements BeforeMM -----> "+BeforeMM.size() );
            System.out.println( "Size/Number of Array Elements AfterMM  -----> "+AfterMM.size() );
            System.out.println("------------------------------------");

            System.out.println("Before Media Folder Conatins----> " + BeforeMM );
            System.out.println("After Media Folder  Conatins----> " + AfterMM );
            System.out.println("------------------------------------");
            //this is where problems start
            List<String> result = AfterMM.stream().filter(elem -> !BeforeMM.contains(elem)).collect(Collectors.toList());
            System.out.println("Result: File(s) present in After BUT NOT BeforeMM folder:\n" + result);
            // Result equals Test2.txt

            /*// Remove all elements in BeforeFolder from AfterFolder
            BeforeMM.removeAll(AfterMM); 
            // Show results
            System.out.println("Result: " + AfterMM);
            */




    }




}

3 个答案:

答案 0 :(得分:2)

它比那简单得多......你可以简单地使用removeAll()

List<String> copied = new ArrayList<>(AfterMM); //copy the list
copied.removeAll(BeforeMM);// this removes from copied everything that's in BeforeMM (if it's also in copied).

然后打印出所有你可以做的事情:

System.out.println("Result: File(s) present in After BUT NOT BeforeMM folder:"+copied);

这将打印copied的内容,其中包含AfterMM但不在BeforeMM

中的所有内容

另外,正如所指出的,您列出了相同的文件夹:

"C:/Users/Jonathan/Desktop/ADP/MagMediaAfter";

然后你的结果将永远是[]。如果您提供path1path2不同的值,您将获得其他结果。

我认为您不能在文件名中包含*,但-不应该造成任何问题。

答案 1 :(得分:2)

我们可以对您的代码进行一些改进和简化,首先您可以使用System.getProperty("user.home")File(String, String)构造函数构建路径。接下来,您可以使用流收集文件列表。然后,您可以使用removeAll来构建最终的List。像,

File desktop = new File(System.getProperty("user.home"), "Desktop");
File adp = new File(desktop, "ADP");
File folder1 = new File(adp, "MagMediaBefore");
File folder2 = new File(adp, "MagMediaAfter");
List<String> beforeMM = Stream.of(folder1.listFiles()) //
        .filter(File::isFile).map(File::getName) //
        .collect(Collectors.toList());
List<String> afterMM = Stream.of(folder2.listFiles()) //
        .filter(File::isFile).map(File::getName) //
        .collect(Collectors.toList());

// Number of Files in each Folder or Directory
System.out.println("Files in FolderA / BeforeMM ----> " //
        + beforeMM.size());
System.out.println("Files in FolderB / AfterMM -----> " //
        + afterMM.size());
System.out.println("------------------------------------");

System.out.println("Before Media Folder Conatins----> " //
        + beforeMM);
System.out.println("After Media Folder  Conatins----> " //
        + afterMM);
System.out.println("------------------------------------");
List<String> result = new ArrayList<>(afterMM);
result.removeAll(beforeMM);
System.out.println("Result: File(s) present in After BUT NOT " //
        + "BeforeMM folder: " + result);

请注意,您目前正在folder1 folder2中阅读相同文件夹。另外,请遵循Java变量命名约定(beforeMMafterMM,你的'看起来像是类名。)

答案 2 :(得分:0)

我没有在你的代码中看到问题,我已经尝试了两个目录的代码,这些目录有大约1K个文件。在任何一点看到,BeforeMM总是超集。结果在两种情况下可能是空的

  • BeforeMM中的文件= AfterMM中的文件

  • AfterMM中的文件= BeforeMM中的文件 - 缺少少量文件