Pom depends on:

<dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.4</version>
</dependency>
Copy the code

Java code:

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/ * * *@Author: zxb
 * @Date : 2020/10/11 5:21 下午
 */
public class MyFileUtil {

    private static List<String> filPathLists = new ArrayList<String>();

    private static final String FILE_PATH = "/Users/zxb/Desktop/doc";

    private static final String DST_PATH = "/Users/zxb/Desktop/copyDIR";

    private static final String EXTENSION_NAME = "pdf";

    private static final String FILE_PATH_NAME = "/Users/zxb/Desktop/doc/test.rtf";

    public static void main(String[] args) {
// List
      
        filePathList = getFileUnderFolderCursively(FILE_PATH, EXTENSION_NAME);
      
// for (int i = 0; i < filePathList.size(); i++) {
// System.out.println(filePathList.get(i));
/ /}
// copyFilesUnderFolderCursively(FILE_PATH, EXTENSION_NAME, DST_PATH);
    }

    /** * Finds all files with the specified suffix in the specified path (non-recursive search) **@paramFilePath File directory *@paramExtensionName File extension */
    public static List<String> getFileUnderFolder(String filePath, String extensionName) {
        List<String> filPathList = new ArrayList<String>();
        File file = new File(filePath);
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if(f.isFile() && f.getName().endsWith(extensionName)) { filPathList.add(f.getAbsolutePath()); }}return filPathList;
    }

    /** * Finds all files with the specified suffix in the specified path (recursive search) **@paramFilePath File directory *@paramExtensionName File extension */
    public static List<String> getFileUnderFolderCursively(String filePath, String extensionName) {
        File file = new File(filePath);
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if (f.isFile() && f.getName().endsWith(extensionName)) {
                filPathLists.add(f.getAbsolutePath());
            } else if(f.isDirectory()) { getFileUnderFolderCursively(f.getAbsolutePath(), extensionName); }}return filPathLists;
    }

    /** * Recursively copies all files with the specified suffix in the specified path to the specified directory **@param filePath
     * @param extensionName
     */
    public static void copyFilesUnderFolderCursively(String filePath, String extensionName, String dstPath) {
        File file = new File(filePath);
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if (f.isFile() && f.getName().endsWith(extensionName)) {
                try {
                    FileUtils.copyFileToDirectory(f, new File(dstPath));
                } catch(IOException e) { e.printStackTrace(); }}else if(f.isDirectory()) { copyFilesUnderFolderCursively(f.getAbsolutePath(), extensionName, dstPath); }}}/** * Read the file contents **@param filePath
     * @paramCharsetName Encoding format *@return* /
    public static String readFileContent(String filePath, String charsetName) {
        String content = "";
        File file = new File(filePath);
        if (file.exists()) {
            Long fileLength = file.length();
            byte[] fileContent = new byte[fileLength.intValue()];
            try {
                FileInputStream in = new FileInputStream(file);
                in.read(fileContent);
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                content = new String(fileContent, charsetName);
            } catch(UnsupportedEncodingException e) { e.printStackTrace(); }}return content;
    }

    /** * Read the contents of the file *@param filePath
     * @param charsetName
     * @return* /
    public static ArrayList<String> readFileContentByLine(String filePath, String charsetName) {
        ArrayList<String> list = new ArrayList<String>();
        String str = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null; // Wrap InputStreamReader to improve processing performance, because BufferedReader is buffered, InputStreamReader is not

        try {
            fis = new FileInputStream(filePath); // FileInputStream
            // Get bytes from a file in the file system
            isr = new InputStreamReader(fis, charsetName);// InputStreamReader is a bridge between the byte stream and the character stream
            br = new BufferedReader(isr);// Reads the contents of the file from the character input stream, encapsulating a new
            while((str = br.readLine()) ! =null) { list.add(str); }}catch (FileNotFoundException e) {
            System.out.println("Unable to find specified file");
        } catch (IOException e) {
            System.out.println("Failed to read file");
        } finally {
            try {
                br.close();
                isr.close();
                fis.close();
            } catch(IOException e) { e.printStackTrace(); }}return list;
    }

    /** * Check whether the file exists **@param filePath
     * @return* /
    public static boolean exists(String filePath) {
        boolean exists = false;
        File file = new File(filePath);
        if (file.exists()) {
            exists = true;
        }
        return exists;
    }

    /** * deletes the specified folder **@param filePath
     */
    public static void deleteDirectory(String filePath) {
        FileUtils.deleteQuietly(new File(filePath));
    }

    /** * Deletes the specified file *@param filePath
     */
    public static void deleteFile(String filePath) {
        try {
            FileUtils.forceDelete(new File(filePath));
        } catch(IOException e) { e.printStackTrace(); }}}Copy the code