如何编写java程序列出两个目录(或文件夹)中的重复文件? [英] How do I write a java program to list the duplicate files in two directories(or folders)?

查看:168
本文介绍了如何编写java程序列出两个目录(或文件夹)中的重复文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个目录或文件夹。我在两个文件夹中都有一些文件,其中一些是重复文件。我想列出这些文件夹中的重复文件。我如何编写一个java程序来执行该任务



我尝试过:



I have 2 directories or folders. I have some files in both folders some of them are duplicate files. I want to list those duplicate files in those folders. How do I write a java program to do that task

What I have tried:

import java.io.File;
 
public class GFG 
{
     static void RecursivePrint(File[] arr,int index,int level) 
     {
         // terminate condition
         if(index == arr.length)
             return;
          
         // tabs for internal levels
         for (int i = 0; i < level; i++)
             System.out.print("\t");
          
         // for files
         if(arr[index].isFile())
             System.out.println(arr[index].getName());
          
         // for sub-directories
         else if(arr[index].isDirectory())
         {
             System.out.println("[" + arr[index].getName() + "]");
              
             // recursion for sub-directories
             RecursivePrint(arr[index].listFiles(), 0, level + 1);
         }
           
         // recursion for main directory
         RecursivePrint(arr,++index, level);
    }

推荐答案

理查德绝对正确:

Richard is absolutely right:
Richard MacCutchan写道:
Richard MacCutchan wrote:

将一个目录中的所有条目读入一个列表。然后对于列表中的每个文件名,查看该文件是否存在于第二个目录中。

Read all the entries in one directory into a list. Then for each filename in the list see if that file exists in the second directory.



最简单的方法是对每个列表进行排序,然后比较名称:

如果A [i]小于B [j]那么A [i]在A中是新的,所以增量i,

如果A [i]等于B [j]它在两个都增加i和j,

其他明智的B [j]是B中的新增加j。


And the simplest way to do it is to sort each list, and then compare names:
if A[i] is less than B[j] then A[i] is new in A so increment i,
if A[i] equals B[j] it's in both so increment i and j,
other wise B[j] is new in B so increment j.


这篇关于如何编写java程序列出两个目录(或文件夹)中的重复文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆