gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:48:00 25 4
gpt4 key购买 nike

我正在尝试比较文件,我的解决方案适用于其中包含少量文件(例如 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);
*/




}




}

最佳答案

我们可以对您的代码进行许多改进和简化,首先您可以使用 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 ,你的看起来像类名)。

关于java - 显示列表数组中的文件名差异 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com