Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回递归方法的文件值_Java_File_Recursion_Return_Hashset - Fatal编程技术网

Java 返回递归方法的文件值

Java 返回递归方法的文件值,java,file,recursion,return,hashset,Java,File,Recursion,Return,Hashset,我想返回递归方法的值,该方法列出目录和子目录中的所有文件。目标是创建md5文件值的哈希集 目前代码运行良好,但只在root dir中运行,而不是在recursive中 static Set<String> localMd5Listing() throws Exception { List<String> localMd5List = new ArrayList<String>(); if(!baseModDirectoryF

我想返回递归方法的值,该方法列出目录和子目录中的所有文件。目标是创建md5文件值的哈希集

目前代码运行良好,但只在root dir中运行,而不是在recursive中

static Set<String> localMd5Listing() throws Exception {

        List<String> localMd5List = new ArrayList<String>();

        if(!baseModDirectoryFile.exists()){
            System.out.println("baseModDirectory doesn't exist, check your baseModDirectory variable.");
        }
        else{
            if(baseModDirectoryFile.isDirectory()){
                File[] paths = baseModDirectoryFile.listFiles();

                if(paths != null){
                    for(File path:paths){
                        if(path.isFile()){
                            FileInputStream fis = new FileInputStream(path);
                            String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
                            fis.close();
                            localMd5List.add(md5);
                        }
                        else if(path.isDirectory()){
                            listChildren(path);
                            //Check md5 for children files as path.isFile condition
                        }

                    }

                }


            }


        }

        Set<String> localSet = new HashSet<String>();
        localSet.addAll(localMd5List);
        localMd5List.clear();
        localMd5List.addAll(localSet);

        return localSet;
    }
不幸的是,我不知道如何链接这两个方法以返回localMd5Listing()中listChildren()的值。我认为在第一个方法中也使用listFile()不是一个好方法


谢谢大家!

您需要的基本设置类似于以下伪代码

public List<string> getAllHashes() {
    List<String> hashes = new List<String>();
    //calculate all the hashes
    foreach(directory d in directories) {
        hashes.add(directory.getAllHashes());
    }
    return hashes;
}
public List getAllHashes(){
列表哈希=新列表();
//计算所有散列
foreach(目录中的目录d){
add(directory.getAllHashes());
}
返回散列;
}

我知道这不是完整的代码,但是有了它,您应该能够进行递归循环。别忘了检查里面是否真的有目录,否则你可能会得到一个空指针

将localMD5listing中的if语句提取到一个方法
recurseMD5
,该方法接受一个文件参数和哈希列表进行更新。然后通过调用

recurseMD5(baseModDirectoryFile, localmd5List);
recurseMD5
中,当参数为目录时,您只需在所有listFiles()上递归。如果,OTOH,它是一个常规文件,则添加md5

void recurseMD5(File it, List<String> hashes) {
     if (it.isDirectory) {
        for (File f : it.listFiles()) recurseMD5(f, hahses);
     }
     else {
         // process MD5 hash of file
     }
}
void recurseMD5(将其归档,列出哈希){
如果(it.isDirectory){
对于(文件f:it.listFiles())递归EMD5(f,hahses);
}
否则{
//处理文件的MD5哈希
}
}

我没有想过创建一个包含两个初始变量(文件和列表)的方法,谢谢你,效果很好!;)有没有想过将这个最终哈希集与另一个哈希集进行比较,并删除链接到第二个哈希集中不包含的哈希的文件?再次感谢你!最后,我使用递归删除方法来删除文件,就像您的示例一样。非常感谢。
void recurseMD5(File it, List<String> hashes) {
     if (it.isDirectory) {
        for (File f : it.listFiles()) recurseMD5(f, hahses);
     }
     else {
         // process MD5 hash of file
     }
}