读取目录中的多个文件并打印特定内容

Reading multiple files in directory and printing specific content

我想要实现的基本上是一个 Java 文件,它查看用户计算机上的特定目录,在目录中的所有文件中搜索特定单词(在本例中为电子邮件),然后最后打印出来。

我现在的当前脚本,查找某个目录中的所有文件,打印出这些文件名。除此之外,我还弄清楚了如何让该脚本在一个文件中搜索特定单词,然后将其打印出来。唯一的问题是,尽管它搜索了那个文件并得到 word/phrase ,但必须为它提供完整的目录和文件才能工作。我只是希望它有一个特定的目录,然后搜索其中的所有文件。我尝试使用我创建的用于查找所有文件的目录变量来执行此操作,但是当将其用作要搜索文件的目录以查找单词时,它不起作用。

下面是我的代码部分,用于我想要的功能。实际函数是在我的真实脚本中调用的,所以不用担心,因为它正在运行。我也刚刚在脚本中评论了我想在哪里工作的变量。

    package aProject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class aScanner {

    static String usernameMac = System.getProperty("user.name");
    final static File foldersMac = new File("/Users/" + usernameMac + "/Library/Mail/V2"); // this is the right directory I want to look through

    public static void listFilesForFolder(final File foldersMac) {
        for (final File fileEntry : foldersMac.listFiles()) {

            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);

                try {
                    BufferedReader bReaderM = new BufferedReader(new FileReader("/Users/username/Library/Mail/V2/AosIMAP-/INBOX.mbox/longnumber-folder/Data/Messages/1.emlx")); //this is where I would like the foldersMac variable to work in, instead of this full directory
                    String lineMe;
                    while((lineMe = bReaderM.readLine()) != null)
                    {
                        if(lineMe.contains(".com"))
                            System.out.println(lineMe);
                    }
                    bReaderM.close();
                }
                catch (IOException e) {

                }

            } else {
                System.out.println(fileEntry.getName());

            }

        }
    }

    }

我认为你的问题在于你的递归逻辑, 你在目录结构中递归地往下走,你遍历你的树,但没有写出任何东西,因为这个 if 语句:

if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);

...
}

早点关闭 If 语句,它应该可以工作。

我认为这就是您要实现的目标:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class aScanner {

    static String usernameMac = System.getProperty("user.name");
    final static File foldersMac = new File("/Users/" + usernameMac + "/Library/Mail/V2");

    public static void main(String[] args) throws IOException {
        listFilesForFolder(foldersMac);
    }

    public static void listFilesForFolder(final File foldersMac) throws IOException {
        for (final File fileEntry : foldersMac.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                ArrayList<String> lines = new ArrayList<>();
                try (BufferedReader bReaderM = new BufferedReader(new FileReader(fileEntry))) {
                    String lineMe;
                    while ((lineMe = bReaderM.readLine()) != null) {
                        if (lineMe.contains(".com")) {
                            lines.add(lineMe);
                        }
                    }
                }
                if (!lines.isEmpty()) {
                    System.out.println(fileEntry.getAbsolutePath() + ":");
                    for (String line : lines) {
                        System.out.println("  " + line.trim());
                    }
                }
            }
        }
    }
}