[英]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. 我试过使用findWithinhorizon函数,但是它似乎仅检查一个单词并且不能接受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.