-1

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

我想做的是比较一个软件的两个不同版本,并找出有多少文件被更改。

4

2 回答 2

-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.
    }
}

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

于 2013-10-29T15:25:52.210 回答
-1

我有这段代码,它将目录中的所有文件与特定文件进行比较,以检查目录中是否存在这些文件,可以根据您的需要进行一些调整。它使用 commons-io.jar

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

import org.apache.commons.io.FileUtils;

public class CompareFile {

    String directory;
    File file;

    public CompareFile(String directory, File file) {
        this.directory = directory;
        this.file = file;
    }



    public boolean doesFileExist() {

        boolean indicatorFileExist = true;
        List<File> files = null;

        try {
            files = getFiles();
            files = files.stream().filter(fileMatch -> {
                try {
                    if(fileMatch.isFile()){
                        return FileUtils.contentEquals(fileMatch, file);
                    }else{
                        return false;
                    }

                } catch (Exception excep) {
                    excep.printStackTrace();
                    return false;
                }
            }).collect(Collectors.toList());

            if(files.isEmpty()){
                indicatorFileExist = false;
            }

        } catch (Exception excep) {
            excep.printStackTrace();
        } finally {
            if (files != null) {
                files = null;
            }
        }

        return indicatorFileExist;

    }

    private List<File> getFiles() {

        List<File> fileList = null;

        try {

            if(directory!=null && directory.trim().length()>0 && file!=null){

                File dir = new File(directory);
                if(dir.isDirectory() && dir.exists() && dir.canRead()){
                    fileList = Arrays.asList(dir.listFiles());
                }


            }

        } catch (Exception excep) {
            excep.printStackTrace();
        }
        return fileList;
    }


}
于 2017-07-08T18:58:53.400 回答