Home > Java > javaTutorial > body text

Java Example - Delete Directory

黄舟
Release: 2017-02-13 09:55:52
Original
1260 people have browsed it

以下实例演示了使用 File 类的 ofdir.isDirectory(), dir.list() 和 deleteDir() 方法在一个个删除文件后删除目录 :

/*
 author by w3cschool.cc
 Main.java
 */import java.io.File;public class Main {
   public static void main(String[] argv) throws Exception {
      deleteDir(new File("c:\\temp"));
   }
   public static boolean deleteDir(File dir) {
      if (dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir            (new File(dir, children[i]));
            if (!success) {
               return false;
            }
         }
      }
      return dir.delete();
      System.out.println("目录已被删除!");
  }}
Copy after login

以上代码运行输出结果为:

目录已被删除!
Copy after login

以上就是Java 实例 - 删除目录的内容,更多相关内容请关注PHP中文网(www.php.cn)!




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!