一尘不染

比较文件名

java

我想比较位于两个不同文件夹中的文件。我只希望比较两个不同文件夹中具有相同名称的文件。

我希望做的是比较一个软件的两个不同版本,并发现已更改了多少文件。


阅读 219

收藏
2020-12-03

共1个答案

一尘不染

这将帮助您获取两个路径的文件:

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

public class ListFiles 
{
    public static void main(String[] args) 
    {

        // First directory path here.
        String path1 = ".";

        // Second directory path here.
        String path2 = ".";

        // File class is very important.
        // If you did a simple Google search
        // Then you would have seen this class mentioned.
        File folder1 = new File(path1);
        File folder2 = new File(path2);

        // It gets the list of files for you.
        File[] listOfFiles1 = folder1.listFiles(); 
        File[] listOfFiles2 = folder2.listFiles();

        // We'll need these to store the file names as Strings.
        ArrayList<String> fileNames1 = new ArrayList<String>();
        ArrayList<String> fileNames2 = new ArrayList<String>();

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

        // Get file names from second directory.
        for (int i = 0; i < listOfFiles2.length; i++) 
        {
            if (listOfFiles2[i].isFile()) 
            {
                fileNames2.add(listOfFiles2[i].getName());
            }
        }

        // Now compare
        // Loop through the two array lists and add your own logic.
    }
}

您将需要添加自己的逻辑进行比较。资源

2020-12-03