如何使用java读取目录中文件的内容并返回具有特定关键字的文件

如何使用java读取目录中文件的内容并返回具有特定关键字的文件,java,java-8,Java,Java 8,在我的代码中,我可以列出我电脑上文件夹中的所有文件,但要检查关键字是否存在于我在StringBuffer中使用indexOf的那些文件中。我面临的问题是,包含该关键字的文件名的期望输出没有得到打印。 我无法找到错误在哪里或我犯了什么错误 导入java.io.File; 导入java.io.IOException; 导入java.util.Scanner; 公共类列表文件{ 公共静态无效主字符串参数[]引发IOException{ //为目录创建文件对象 File directoryPath=ne

在我的代码中,我可以列出我电脑上文件夹中的所有文件,但要检查关键字是否存在于我在StringBuffer中使用indexOf的那些文件中。我面临的问题是,包含该关键字的文件名的期望输出没有得到打印。 我无法找到错误在哪里或我犯了什么错误

导入java.io.File; 导入java.io.IOException; 导入java.util.Scanner; 公共类列表文件{ 公共静态无效主字符串参数[]引发IOException{ //为目录创建文件对象 File directoryPath=new FileC:\\Users\\nanis\\Downloads\\new folder; //所有文件和目录的列表 文件filesList[]=directoryPath.listFiles; //System.out.println指定目录中的文件和目录列表:; 扫描仪sc=空; 对于文件:filesList{ //System.out.printlnFile name:+file.getName; //System.out.printlnFile路径:+file.getAbsolutePath; //System.out.printlnSize:+file.getTotalSpace; //实例化Scanner类 sc=新扫描文件; 字符串输入; StringBuffer sb=新的StringBuffer; 而sc.hasNextLine{ 输入=sc.nextLine; 某人输入+; int integer=sb.indexOfVM;//关键字是我要搜索的VM 如果整数>0{ System.out.println关键字出现在+file.getAbsolutePath中 } } } } }
我想问题出在你的扫描仪上。您正在获取用户输入=sc.nextLine;要做到这一点,您需要System.in。另外,如果我尝试使用sc打印某些内容,它将不打印任何内容,这将使sc.hasNextLine为false,因此您的算法有问题

你正在做一个搜索算法,对吗?但是您正在for each循环中插入while循环。这不起作用,因为在for-each循环的第一次迭代中,只有第一个文件存在,因此如果您的搜索关键字是最后一个文件,那么它将为false

基本搜索算法是:

1. Input keyword to find
2. Loop through the lists to check if found
3. return either true or false
编辑:您要搜索.txt文件。这是相同的概念。您需要先将它们全部存储起来。存储时不进行搜索。如果您已存储它们,现在可以搜索它们

我将代码改为读取txt文件。此外,您的目录应该只包含.txt文件,否则它将无法工作,因为您正在读取文件的内容。在您的情况下,HashMap会很有用,因为您需要存储两个值。文件名及其内容

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public class ListOfFiles {

    public static void main(String args[]) throws IOException {
        // Creating a File object for directory
        File directoryPath = new File("C:\\Users\\nanis\\Downloads\\New folder\\");
        // List of all files and directories
        File filesList[] = directoryPath.listFiles();
        // System.out.println("List of files and directories in the specified
        // directory:");
        Scanner sc = new Scanner(System.in);
        Scanner myReader = null;
        HashMap<String, String> fff = new HashMap<String, String>(); // storage for the file name and content

        for (File file : filesList) {
            File myObj = new File("C:\\Users\\nanis\\Downloads\\New folder\\" + file.getName());
            myReader = new Scanner(myObj);
            String read = "";
            while (myReader.hasNextLine()) {
                read += myReader.nextLine();
            }
            fff.put(file.getName(), read); // store file name and its contents
        }

        System.out.print("Search The File By Keyword:");
        String find = sc.nextLine();
        for (String i : fff.keySet()) {
            if (fff.get(i).contains(find)) { // check the contents if it contains the keyword u are search for
                File found = new File("C:\\Users\\nanis\\Downloads\\New folder\\" + i);
                System.out.println("keyword is present in " + found.getAbsolutePath());
            }
        }

        sc.close();
        myReader.close();
    }
}

谢谢您的时间,但我的问题是,如果关键字出现在文件内容中,那么它必须打印这些文件名。在代码中,在文件名中搜索关键字。但是谢谢你的努力。你打算只读取.txt文件吗?是的,我打算读取目录中的.txt、.docx、.pdf文件。@xopy。我已经编辑了我的答案,但这只适用于txt文件。阅读docx和pdf将使其更加复杂。我建议阅读其他支持将所有行附加到StringBuffer并在整个缓冲区中重复搜索关键字的API。只需在读取后立即搜索特定的行输入。此外,关键字可能位于行的开头,因此您应该使用>=0作为索引。或者首先使用ifinput.containsSVM。你应该在声明中使用扫描仪。