【问题标题】:How to search entire hard drive for a specific file (Java Eclipse)?如何在整个硬盘驱动器中搜索特定文件(Java Eclipse)?
【发布时间】:2020-06-02 09:21:41
【问题描述】:
    public static void fileSearcher() throws IOException {
    File dire = new File ("C:/");
    String[] allFile = dire.list();
    for (int i = 0;i<10;i++) {            
        String FilIn = allFile[i];                   // here is where I need to scan entire pc for file
        if(FilIn.equals("eclipse.exe")){
            System.out.println("Found Eclipse!");
            break;
        }
        else {
            System.out.println("Eclipse not found on local drive.");
        }

是否有一种简单的方法可以扫描整个 HHD/SSD 以查找特定文件?

【问题讨论】:

  • 使用Files.walkFiles.walkFileTree。阅读javadocs 了解更多信息。
  • a) 您需要遍历所有文件,而不仅仅是前 10 个文件。 b) 使用listFiles() 而不是list() 来获得File[]。 c) 你需要检查文件是否是一个目录(如果你做了b),这很容易)并递归地重复该文件夹的搜索过程。
  • @f1sh 有比这更简单的方法......从 Java 7 开始。
  • @StephenC 没错,但这看起来像是一个需要“手动”遍历的作业。

标签: java eclipse scanning


【解决方案1】:

这将为您完成这项工作,并且在有多个驱动器或分区时也可以使用。它还将搜索具有相同名称的其他文件。

import java.io.File;
import javax.swing.filechooser.FileSystemView;

public class HardDiskSearcher {

    private static boolean fileFound = false;

    private static String searchTerm = "eclipse.exe";

    public static void main(String[] args) {

        fileFound = false;

        File[] systemRoots = File.listRoots();
        FileSystemView fsv = FileSystemView.getFileSystemView();

        for (File root: systemRoots) {
            if (fsv.getSystemTypeDescription(root).equals("Local Disk")) {
                File[] filesFromRoot = root.listFiles();
                recursiveSearch(searchTerm, filesFromRoot);
            }
        }

        System.out.println("File you searched for was found?  : " + fileFound);

    }

    private static void recursiveSearch(String searchTerm, File... files) {

        for (File file: files) {

            if (file.isDirectory()) {
                File[] filesInFolder = file.listFiles();

                if (filesInFolder != null) {
                    for (File f : filesInFolder) {
                        if (f.isFile()) {
                            if (isTheSearchedFile(f, searchTerm)) {
                                fileFound = true;
                            }
                        }   
                    }

                    for (File f : filesInFolder) {
                        if (f.isDirectory()) {
                            recursiveSearch(searchTerm, f);
                        }
                    }
                }
            }
            else if (isTheSearchedFile(file, searchTerm)) {
                fileFound = true;
            }
        }
    }

    private static boolean isTheSearchedFile(File file, String searchTerm) {
        if (file.isFile() && (searchTerm.equals(file.getName())) ) {
            System.out.println("The file you searched for has been found! " +
                    "It was found at: " + file.getAbsolutePath());
            return true;
        }
        return false;
    }

}

我不是 100% 确定为什么这里需要 if 语句 (filesInFolder != null),但我怀疑 Windows 在尝试从特定系统文件夹或其他内容中列出文件时返回 null。

【讨论】:

  • 我刚试了但是什么也没有出现,我知道它从任务管理器中扫描磁盘但是程序停止运行时最终什么也没出现。
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 2015-08-22
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 2010-09-29
相关资源
最近更新 更多