【问题标题】:How to read content of files in directory using java and return the files which have particular keyword如何使用java读取目录中文件的内容并返回具有特定关键字的文件
【发布时间】:2021-04-12 13:18:50
【问题描述】:

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

import java.io.File;
import java.io.IOException;
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 = null;
        
        for (File file: filesList) {
            // System.out.println("File name: "+file.getName());
            // System.out.println("File path: "+file.getAbsolutePath());
            // System.out.println("Size :"+file.getTotalSpace());
            // Instantiating the Scanner class
            sc = new Scanner(file);
            String input;
            StringBuffer sb = new StringBuffer();
            
            while (sc.hasNextLine()) {
                input = sc.nextLine();
                sb.append(input + " ");

                int integer = sb.indexOf("VM"); // the keyword is "VM" that I want to search
                if (integer > 0) {
                    System.out.println("keyword is present in " + file.getAbsolutePath())
                }
            }
        }
    }
}

【问题讨论】:

  • 将所有行附加到 StringBuffer重复在整个缓冲区中搜索关键字是没有意义的。只需在阅读后立即搜索特定行 (input)。此外,关键字可能位于行首,因此您应该使用>= 0 作为索引。或者首先使用if(input.contains("VM")) …。你应该在try(…) 语句中使用扫描器。

标签: java java-8


【解决方案1】:

我认为问题出在您的扫描仪上。您正在接受用户输入input = sc.nextLine();,为此您需要System.in。此外,如果我尝试使用 sc 打印某些内容,它不会打印任何内容,这会使 while (sc.hasNextLine()) 错误,因此您的算法有问题。

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

一个基本的搜索算法是:

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 会使其更加复杂。我建议阅读其他支持该 API 的 API
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2012-08-19
相关资源
最近更新 更多