简体   繁体   中英

How to search entire hard drive for a specific file (Java Eclipse)?

    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.");
        }

Is there a simple way to scan an entire HHD/SSD for a specific file?

This will do the job for you and it works too when there are multiple drives or partitions. It will also search for additional files with that same name.

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;
    }

}

I am not 100% sure about why the if-statement (filesInFolder.= null) is necessary here but i suspect Windows returns null when trying to list files from specific system folder or something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM