📜  用于在目录中搜索文件的Java程序

📅  最后修改于: 2022-05-13 01:54:52.625000             🧑  作者: Mango

用于在目录中搜索文件的Java程序

可以使用 File 类和 FilenameFilter 接口在Java中搜索文件。 FilenameFilter 接口用于从文件列表中过滤文件。该接口有一个boolean accept(File dir, String name)方法,用于从Java.io.File.list() 方法返回的列表中查找所需的文件。当我们想在文件夹中查找具有特定扩展名的文件时,此方法非常有用。

第一种方法

  1. 创建一个 MyFilenameFilter 类,它实现 FilenameFilter 接口并覆盖 FilenameFilter 接口的 accept() 方法。
  2. accept() 方法有两个参数,第一个是目录名,第二个是文件名。
  3. 如果文件名以指定的首字母开头,accept() 方法返回 true,否则返回 false。
  4. FindFile 类包含 main 方法,该方法接受用户输入,例如要搜索的所需目录和要搜索的文件的首字母。
  5. File类的目录对象以导演名发起,MyFilenameFilter类的过滤对象以用户提供的首字母发起。
  6. list() 方法在 dir 对象上调用,该对象返回满足条件的文件数组。
  7. 迭代数组并将所需文件的名称打印到输出屏幕。

代码实现

Java
// Java Program to Search for a File in a Directory
import java.io.*;
  
// MyFilenameFilter class implements FilenameFilter
// interface
class MyFilenameFilter implements FilenameFilter {
    
    String initials;
    
    // constructor to initialize object
    public MyFilenameFilter(String initials)
    {
        this.initials = initials;
    }
    
    // overriding the accept method of FilenameFilter
    // interface
    public boolean accept(File dir, String name)
    {
        return name.startsWith(initials);
    }
}
  
public class Main {
    
    public static void main(String[] args)
    {
        // Create an object of the File class
        // Replace the file path with path of the directory
        File directory = new File("/home/user/");
  
        // Create an object of Class MyFilenameFilter
        // Constructor with name of file which is being
        // searched
        MyFilenameFilter filter
            = new MyFilenameFilter("file.cpp");
  
        // store all names with same name 
        // with/without extension
        String[] flist = directory.list(filter);
  
        // Empty array
        if (flist == null) {
            System.out.println(
                "Empty directory or directory does not exists.");
        }
        else {
  
            // Print all files with same name in directory
            // as provided in object of MyFilenameFilter
            // class
            for (int i = 0; i < flist.length; i++) {
                System.out.println(flist[i]+" found");
            }
        }
    }
}


Java
// Java Program to Search for a File in a Directory
import java.io.File;
  
public class Main {
    
    public static void main(String[] argv) throws Exception
    {
        // Create an object of the File class
        // Replace the file path with path of the directory
        File directory = new File("/home/user/");
  
        // store all names with same name
        // with/without extension
        String[] flist = directory.list();
        int flag = 0;
        if (flist == null) {
            System.out.println("Empty directory.");
        }
        else {
  
            // Linear search in the array
            for (int i = 0; i < flist.length; i++) {
                String filename = flist[i];
                if (filename.equalsIgnoreCase("file.cpp")) {
                    System.out.println(filename + " found");
                    flag = 1;
                }
            }
        }
  
        if (flag == 0) {
            System.out.println("File Not Found");
        }
    }
}


输出

file.cpp found

第二种方法

  1. list() 方法在 File 类的 dir 对象和 'flist' 数组中的文件列表上调用。
  2. 'flist' 数组中的每个文件都根据所需的文件名进行检查。
  3. 如果找到匹配项,则将其打印在屏幕上。

这种方法与前一种方法略有不同,因为在这种情况下,用户需要指定文件的确切名称。

代码实现

Java

// Java Program to Search for a File in a Directory
import java.io.File;
  
public class Main {
    
    public static void main(String[] argv) throws Exception
    {
        // Create an object of the File class
        // Replace the file path with path of the directory
        File directory = new File("/home/user/");
  
        // store all names with same name
        // with/without extension
        String[] flist = directory.list();
        int flag = 0;
        if (flist == null) {
            System.out.println("Empty directory.");
        }
        else {
  
            // Linear search in the array
            for (int i = 0; i < flist.length; i++) {
                String filename = flist[i];
                if (filename.equalsIgnoreCase("file.cpp")) {
                    System.out.println(filename + " found");
                    flag = 1;
                }
            }
        }
  
        if (flag == 0) {
            System.out.println("File Not Found");
        }
    }
}

输出

file.cpp found