简体   繁体   English

通过目录及其子目录中的所有文件搜索存储在数组列表中的值

[英]searching for values stored in an arraylist through all the files within a directory and its sub directories

I am creating a program in java that searches for a word in all the files in a directory and lists them. 我正在用Java创建一个程序,该程序在目录中的所有文件中搜索单词并将其列出。 I have tried using findWithinhorizon function however it seems that it checks only for one word and cannot accept an arraylist as a parameter. 我试过使用findWithinhorizo​​n函数,但是它似乎仅检查一个单词并且不能接受arraylist作为参数。 This is want I am trying to do: There is an array list containing values say(word1, word2, word3, word4). 这是我要尝试执行的操作:有一个数组列表,其中包含值say(word1,word2,word3,word4)。 I want to search all the files for these words in the directory that i specify and its sub directories and list all the file names along with the path that contain these words. 我想在我指定的目录及其子目录中的所有文件中搜索这些词,并列出所有文件名以及包含这些词的路径。

Example: it takes the first value from the array list ie word1 look for the files containing word1 in the directory(and its sub directories) that I specified and list all the files. 示例:它从数组列表中获取第一个值,即word1在我指定的目录(及其子目录)中查找包含word1的文件,并列出所有文件。 then it takes the 2nd word from array list word2 searches it in the directory and lists all the files and so on. 然后它从数组列表中取第二个单词word2在目录中搜索它并列出所有文件,依此类推。

any help is appreciated. 任何帮助表示赞赏。 thanks. 谢谢。

List all the files in a directory. 列出目录中的所有文件。

`public void listFilesAndFilesSubDirectories(String directoryName){ `public void listFilesAndFilesSubDirectories(String directoryName){

    File directory = new File(directoryName);

    //get all the files from a directory
    File[] fList = directory.listFiles();

    for (File file : fList){
        if (file.isFile()){
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()){
            listFilesAndFilesSubDirectories(file.getAbsolutePath());
        }
    }
}`

Now loop through your list of files reading each one. 现在循环遍历读取每个文件的文件列表。 Now iterate the list containing the words, check with each word. 现在,迭代包含单词的列表,并检查每个单词。 If you find the word, add the file name in another list. 如果找到单词,请在另一个列表中添加文件名。

See to that you iterate the file in the outer loop and the word inside the file loop. 确保在外部循环中迭代文件,并在文件循环中迭代单词。

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

相关问题 如何列出目录及其所有子目录中的所有文件 - How to list all files within a directory and all its sub-directories 读取目录中的所有文件,包括其子目录 - Reading all files in a directory including its sub directories 如何复制目录及其子目录,文件和zip文件 - How to copy a directory with its sub directories , files and zip files 如何从目录和子目录复制具有某些扩展名的所有文件? - How to copy all files with certain extensions from a directory and sub directories? Eclipse在没有文件的Antlr生成的代码目录上显示警告,但在其子目录上不显示警告 - Eclipse shows warning on Antlr generated code directory with no files, but not on its sub-directories 递归监视Java中的目录和所有子目录 - Recursively monitor a directory and all sub directories in java 在Arraylist的Arraylist中搜索 - Searching through Arraylist of Arraylist SimpleFileVisitor 遍历目录树以查找除两个子目录之外的所有 .txt 文件 - SimpleFileVisitor to walk a directory tree to find all .txt files except in two sub directories 列出目录中所有文件(包括子目录)的最有效方法是什么? - What is the most efficient way to list all of the files in a directory (including sub-directories)? 在目录中搜索文件及其在Java中的子目录 - Search files in directory and its sub directory in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM