zoukankan      html  css  js  c++  java
  • Delete Directory Recursively

    How to delete a directory recursively with all its subdirectories and files in Java

    In this short article, you’ll learn how to delete a directory recursively along with all its subdirectories and files.

    There are two examples that demonstrate how to achieve this task. The idea behind both of the examples is to traverse the file tree, and delete the files in any directory before deleting the directory itself.

    Delete directory recursively - Java 8+

    This example makes use of Files.walk(Path) method that returns a Stream<Path> populated with Path objects by walking the file-tree in depth-first order.

    package com.baeldung;
    
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Comparator;
    
    public class DeleteDirectoryRecursively {
    
        public static void main(String[] args) throws IOException {
            
            Path dir = Paths.get("java");
    
            // Traverse the file tree in depth-first fashion and delete each file/directory
            Files.walk(dir)
                    .sorted(Comparator.reverseOrder())
                    .forEach(path -> {
                        try {
                            System.out.println("Deleting : " + path);
                            Files.delete(path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        }
    }
    

    Delete directory recursively - Java 7

    The following example uses Files.walkFileTree(Path, FileVisitor) method that traverses a file tree and invokes the supplied FileVisitor for each file.

    We use a SimpleFileVisitor to perform the delete operation.

    package com.zetcode;
    
    import java.io.IOException;
    import java.nio.file.*;
    import java.nio.file.attribute.BasicFileAttributes;
    
    public class DeleteDirectoryRecursively {
    
        public static void main(String[] args) throws IOException {
            Path dir = Paths.get("java");
    
            // Traverse the file tree and delete each file/directory.
            Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println("Deleting file: " + file);
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    System.out.println("Deleting dir: " + dir);
                    if (exc == null) {
                        Files.delete(dir);
                        return FileVisitResult.CONTINUE;
                    } else {
                        throw exc;
                    }
                }
            });
        }
    }
    
  • 相关阅读:
    mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案
    WebAPI 用户认证防篡改实现HMAC(二)签名验证 AbsBaseAuthenticationAttribute--转
    Calling a Web API From a .NET Client (C#)
    http状态码有那些?分别代表是什么意思
    html 之表单,div标签等。。。。。。。
    mysql之视图,触发器,事务等。。。
    pymysql模块
    MySQL之IDE工具介绍及数据备份
    mysql老是停止运行该怎么解决
    前端基础之html
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13098826.html
Copyright © 2011-2022 走看看