java刪除文件夾及子目錄

package test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;

public class DelFile {

    /**
     * 刪除某個文件夾下的全部文件夾和文件
     * 
     * @param delpath
     *            String
     * @throws FileNotFoundException
     * @throws IOException
     * @return boolean
     */
    public static boolean deletefile(String delpath) throws Exception {
        try {

            File file = new File(delpath);
            // 當且僅當此抽象路徑名錶示的文件存在且 是一個目錄時,返回 true
            if (!file.isDirectory()) {
                file.delete();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + "\\" + filelist[i]);
                    if (!delfile.isDirectory()) {
                        delfile.delete();
                        System.out.println(delfile.getAbsolutePath() + "刪除文件成功");
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + "\\" + filelist[i]);
                    }
                }
                System.out.println(file.getAbsolutePath() + "刪除成功");
                file.delete();
            }

        } catch (FileNotFoundException e) {
            System.out.println("deletefile() Exception:" + e.getMessage());
        }
        return true;
    }

    /**
     * 輸出某個文件夾下的全部文件夾和文件路徑
     * 
     * @param delpath
     *            String
     * @throws FileNotFoundException
     * @throws IOException
     * @return boolean
     */
    public static boolean readfile(String filepath)
            throws FileNotFoundException, IOException {
        try {

            File file = new File(filepath);
            System.out.println("遍歷的路徑爲:" + file.getAbsolutePath());
            // 當且僅當此抽象路徑名錶示的文件存在且 是一個目錄時(即文件夾下有子文件時),返回 true
            if (!file.isDirectory()) {
                System.out.println("該文件的絕對路徑:" + file.getAbsolutePath());
                System.out.println("名稱:" + file.getName());
            } else if (file.isDirectory()) {
                // 獲得目錄中的文件和目錄
                String[] filelist = file.list();
                if (filelist.length == 0) {
                    System.out.println(file.getAbsolutePath()
                            + "文件夾下,沒有子文件夾或文件");
                } else {
                    System.out
                            .println(file.getAbsolutePath() + "文件夾下,有子文件夾或文件");
                }
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    System.out.println("遍歷的路徑爲:" + readfile.getAbsolutePath());
                    if (!readfile.isDirectory()) {
                        System.out.println("該文件的路徑:"
                                + readfile.getAbsolutePath());
                        System.out.println("名稱:" + readfile.getName());
                    } else if (readfile.isDirectory()) {
                        System.out.println("-----------遞歸循環-----------");
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }

        } catch (FileNotFoundException e) {
            System.out.println("readfile() Exception:" + e.getMessage());
        }
        return true;
    }

    public static void main(String[] args) {
        try {
            // readfile("D:/file");
            deletefile("E:/a");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("ok");
    }

}
相關文章
相關標籤/搜索