简体   繁体   English

在特定目录中查找文件名并存储在数组列表中的代码

[英]A code that looks for a file name in a specific directory and store in in the array list

Good morning everyone, 大家,早安,

I'm stuck here with a code that I'm trying to make work. 我被困在这里的代码,我试图使之工作。 The goal of this code is to make a recursive algorithm to find all files that matched a target file name. 此代码的目标是创建一个递归算法,以查找与目标文件名匹配的所有文件。 Each string should store the pathname to the matching file. 每个字符串应将路径名存储到匹配文件。 Return null if no matching files are found. 如果找不到匹配的文件,则返回null。 Here is what I have so far: 这是我到目前为止的内容:

import java.io.File;
import java.util.ArrayList;

public class ArrayListFiles 
{
    public static ArrayList searchForFile
    (File dir, ArrayList filePaths, String target)
    {
        if (!dir.isDirectory())
            return null;
        else 
        {
            File[] files=dir.listFiles();
            for (int index = 0; index < filePaths; index++)
            {
                if(files[index].isFile())
                {
                    if(files[index].getName().equals(target))
                    {
                        filePaths.add(files[index].toString());
                    }
                }
            else if(files[index].isDirectory())
                searchForFile(files[index], filePaths, target);
            }
        }
    return filePaths;
    }
    public static void main(String[] args)
    {
        ArrayList filePaths = new ArrayList();
        File rootFolder = new File("F:\\java");
        String targetFile = "sample.txt";
        filePaths = searchForFile(rootFolder, filePaths, targetFile);
        if(filePaths==null)
            System.out.println("No file is found");
        else
        {
            for (String string : filePaths)
            {
                System.out.println(string);
            }
        }

    }
}

I am very new to programming and apologize in advance if I'm missing something very obvious. 我对编程非常陌生,如果我遗漏了一些非常明显的内容,请提前道歉。 Thank you in advance and have a great day! 在此先感谢您,祝您生活愉快!

To resolve this error "Type mismatch: cannot convert from element type Object to String" for "for (String string : filePaths). " change your code to this : 解决此错误, "Type mismatch: cannot convert from element type Object to String" for "for (String string : filePaths). ”的代码更改"Type mismatch: cannot convert from element type Object to String" for "for (String string : filePaths).

ArrayList<String> filePaths = new ArrayList<String>();
        File rootFolder = new File("F:\\java");
        String targetFile = "sample.txt";
        filePaths = searchForFile(rootFolder, filePaths, targetFile);
        if(filePaths==null)
            System.out.println("No file is found");
        else
        {
            for (String string : filePaths)
            {
                System.out.println(string);
            }
        }

FilePaths is an arraylist and it should be as follows : FilePaths是一个arraylist,它应如下所示:

 for (int index = 0; index < files.length; index++)

Hope this helps . 希望这可以帮助 。

This is probably your problem: 这可能是您的问题:

int index = 0; index < filePaths; index++

FilePaths is an arraylist and you're using it as integer. FilePaths是一个数组列表,您将其用作整数。 You probably wanted to iterate through files? 您可能想遍历文件? Try 尝试

int index = 0; index < files.length; index++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM