java删除文件和文件夹

来源:互联网 发布:js和java做验签 编辑:程序博客网 时间:2024/03/29 14:39
/**     * 删除文件夹     *      * @param pathName     *            To delete the directory or file     * @return Delete successful return True, otherwise return False     * @author andy.z     */    static public boolean deleteFolder(String pathName) {        boolean flag = false;        File file = new File(pathName);        if (file.exists()) {            if (file.isFile())                deleteFile(pathName);            else                deleteDirectory(pathName);            flag = true;        }        return flag;    }    /**删除路径     * Delete directories (folders) and files in that directory     *      * @param sPath     *            To delete a directory file path     * @return The directory deleted successfully return True, otherwise return     *         False     * @author andy.z     */    static public boolean deleteDirectory(String sPath) {        boolean flag = false;        // sPath not the end of the file separator automatically add the file        // separator不是分隔符,自动添加分隔符        if (!sPath.endsWith(File.separator)) {            sPath = sPath + File.separator;        }        File dirFile = new File(sPath);        // If the dir the corresponding file does not exist or is not a        // directory, then exit如果目录不存在或者不是目录就退出        if (!dirFile.exists() || !dirFile.isDirectory()) {            flag = false;        }        //删除文件及子文件 Delete folder all the files (including subdirectories)        File[] files = dirFile.listFiles();        for (int i = 0; i < files.length; i++) {            if (files[i].isFile()) {// delete file                flag = deleteFile(files[i].getAbsolutePath());                if (!flag)                    break;            } else {// delete sub_dir                flag = deleteDirectory(files[i].getAbsolutePath());                if (!flag)                    break;            }        }        if (!flag)            flag = false;        if (dirFile.delete()) {// delete current dir            flag = true;        } else {            flag = false;        }        return flag;    }    /**     * 删除文件     *      * @param targerFile     *            Point you want to delete the target file     * @return The single file deleted successfully returns true, false     *         otherwise     * @author andy.z     */    static public boolean deleteFile(String targerFile) {        boolean flag = false;        File file = new File(targerFile);        if (file.isFile() && file.exists()) {            file.delete();            flag = true;        }        return flag;    }    /**删除文件或者文件夹     * Delete the files under the specified path (folder)     *      * @param path     *            specified path     * @author andy.z     */    public static void deleteFileOrFolder(String path) {        File file = new File(path);        if (!file.exists()) {            return;        } else {            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {                if (files[i].isFile()) {                    files[i].delete();                    System.out.println("已删除文件:" + files[i].getName());                } else {                    deleteDirectory(files[i].getAbsolutePath());                    System.out.println("已删除文件夹:" + files[i].getName());                }            }        }    }
0 0
原创粉丝点击