package com.jtyjy.pdf2pptx.support;

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

public class FileUtil {

    /**
     * 获取某个文件夹下的所有文件全路径名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllFilePathName(String path) {
        List<String> fileNameList = new ArrayList<String>();
        boolean flag = false;
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                fileNameList.add(tempList[i].getAbsolutePath());
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return fileNameList;
    }

    /**
     * 获取某个文件夹下的所有文件全路径名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<File> getAllFiles(String path) {
        List<File> fileList = new ArrayList<>();
        boolean flag = false;
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                fileList.add(tempList[i]);
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return fileList;
    }

    /**
     * 获取某个文件夹下的所有文件名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllFileName(String path) {
        List<String> fileNameList = new ArrayList<String>();
        boolean flag = false;
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                fileNameList.add(tempList[i].getName());
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return fileNameList;
    }

    /**
     * 获取某个文件夹下的所有文件夹全路径名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllDirPathName(String path) {
        List<String> dirNameList = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isDirectory()) {
                dirNameList.add(tempList[i].getAbsolutePath());
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return dirNameList;
    }

    /**
     * 获取某个文件夹下的所有文件夹名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllDirName(String path) {
        List<String> dirNameList = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isDirectory()) {
                dirNameList.add(tempList[i].getName());
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return dirNameList;
    }

    /**
     * 获取某个文件夹下的所有文件夹名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllDirNameRe(String path, List<String> dirNameList) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        if (tempList != null) {
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isDirectory()) {
                    dirNameList.add(tempList[i].getName());
                    getAllDirNameRe(tempList[i].getAbsolutePath(), dirNameList);
                }
            }
        }
        return dirNameList;
    }

    /**
     * 获取某个文件夹下的所有文件夹
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<File> getAllDirs(String path) {
        List<File> dirNameList = new ArrayList<File>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isDirectory()) {
                dirNameList.add(tempList[i]);
            }
            /*if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }*/
        }
        return dirNameList;
    }

    /**
     * 获取某个文件夹下的所有文件全路径名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllFilePathNameRe(String path, List<String> fileNameList) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        if (tempList != null) {
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isFile()) {
                    fileNameList.add(tempList[i].getAbsolutePath());
                }
                if (tempList[i].isDirectory()) {
                    getAllFilePathNameRe(tempList[i].getAbsolutePath(), fileNameList);
                }
            }
        }
        return fileNameList;
    }

    /**
     * 获取某个文件夹下的所有文件夹全路径名
     *
     * @param path 文件夹的路径
     * @return
     */
    public static List<String> getAllDirPathNameRe(String path, List<String> dirNameList) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        if (tempList != null) {
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isDirectory()) {
                    dirNameList.add(tempList[i].getAbsolutePath());
                    getAllDirPathNameRe(tempList[i].getAbsolutePath(), dirNameList);
                }
            }
        }
        return dirNameList;
    }

    //删除指定文件夹下所有文件
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    //删除文件夹
    //param folderPath 文件夹完整绝对路径

    public static boolean delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            return myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {
        List<String> allDirPathName = getAllDirPathName("F:\\pdf图片");
        for (String s : allDirPathName) {
            System.out.println(s);
        }
    }


}
