This is the 16th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

Copy a low-grade version of Everything in Java

What is “Everything”?

“Everything” is a search engine on Windows that allows you to quickly locate files and folders based on file names.

Unlike Windows built-in search, “Everything” shows every file and folder on the computer (just like its name “Everything”) by default.

The keywords you enter in the search box will filter the files and folders displayed.

In general, Everything is a free Windows application that allows you to quickly retrieve files and folders and uses less resources.

Make a low-spec version of “Everything”

Let’s make a low-spec version of “Everything” in Java today!

Let’s start with the main function:

public static void main(String[] args) {
    System.out.println("* * * * * * * * * * * * * * * * * * * * *");
    System.out.println("Welcome To Everything");
    System.out.println("* * * * * * * * * * * * * * * * * * * * *");
    
    System.out.println("Please enter the name of the file you want to find:");
    String fileName=scanner.next();
    
    System.out.println("File name \t File path \t file size \t File last modified");
    
    File[] Drive_letters=File.listRoots();
    for(File dl:Drive_letters) { find(dl,fileName); }}Copy the code

The graphical interface is not used here. The command line interface is used first, and the prompt is simply output at the beginning.

Then enter the name of the file or folder to look for, and since Everything runs only under Windows, we only consider looking for files in Windows. (Secretly happy ~)

Windows filesystems may have multiple root directories, so we use file.listRoots (). Let’s introduce file.listRoots ().

Introduction to the File. ListRoots ()

Java supports zero or more file systems. Each file system has a {@code root} directory from which all other files in that file system can be accessed. For example, the Windows platform has a root directory for each active drive; UNIX platforms have a root directory, which is {@code “/”}. The available file system root set is affected by various system-level operations, such as the insertion or eject of removable media and the disconnection or uninstallation of physical or virtual disk drives.

This method returns an array of {@code File} objects representing the root directory of the available File system. You can guarantee that the canonical pathname of any file that is physically present on the local machine can start with one of the roots returned by this method (for example, C, D, E on Windows).

Use file.listroots () to get the various root directories of the computer’s File system, and then look for the files in each root separately.

Now take a look at the function we wrote to find the file.

public static void find(File file, String fileName) {
        if (null! = file) {if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (null! = files && files.length >0) {
                    for(File f : files) { find(f, fileName); }}}else {
​
                String fName = file.getName();
​
​
                if (fName.contains(fileName)) {
                    String fileResultName = file.getName();
                    String fileResultPath = file.getAbsolutePath();
                    long fileResultSize = file.length();
                    long fileResultModify = file.lastModified();
​
                    System.out.println(fileResultName + "\t" + fileResultPath + "\t" + fileResultSize + "\t"+ fileResultModify); }}}}Copy the code

The first step is to determine if a file or folder, which may be hidden or protected, is empty.

Then determine if the file is a folder. If it is, use file.listFiles() to get an array of file types.

Then determine whether the folder is empty, if so, do not enter. If it is not empty, iterate through the array of file types and continue the search each time using recursion.

If the file is not a folder, you don’t need to recurse.

We check whether the file name is the same as the one we are looking for. If so, we print the file or folder filename, folder name, absolute path, file folder size and last modified time.

The End..

See you~