zoukankan      html  css  js  c++  java
  • 转载:Java 删除文件夹和子文件夹中的所有文件

    转载网址:http://blog.csdn.net/qinpeng100423/article/details/6896198

    1. import java.io.FileNotFoundException;  
    2. import java.io.IOException;  
    3. import java.io.File;  
    4.   
    5. public class ReadFile {  
    6.   
    7.  /** 
    8.   * 删除某个文件夹下的所有文件夹和文件 
    9.   * 
    10.   * @param delpath 
    11.   *            String 
    12.   * @throws FileNotFoundException 
    13.   * @throws IOException 
    14.   * @return boolean 
    15.   */  
    16.  public static boolean deletefile(String delpath) throws Exception {  
    17.   try {  
    18.   
    19.    File file = new File(delpath);  
    20.    // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true  
    21.    if (!file.isDirectory()) {  
    22.     file.delete();  
    23.    } else if (file.isDirectory()) {  
    24.     String[] filelist = file.list();  
    25.     for (int i = 0; i < filelist.length; i++) {  
    26.      File delfile = new File(delpath + "\\" + filelist[i]);  
    27.      if (!delfile.isDirectory()) {  
    28.       delfile.delete();  
    29.       System.out  
    30.         .println(delfile.getAbsolutePath() + "删除文件成功");  
    31.      } else if (delfile.isDirectory()) {  
    32.       deletefile(delpath + "\\" + filelist[i]);  
    33.      }  
    34.     }  
    35.     System.out.println(file.getAbsolutePath()+"删除成功");  
    36.     file.delete();  
    37.    }  
    38.   
    39.   } catch (FileNotFoundException e) {  
    40.    System.out.println("deletefile() Exception:" + e.getMessage());  
    41.   }  
    42.   return true;  
    43.  }  
    44.   
    45.  /** 
    46.   * 输出某个文件夹下的所有文件夹和文件路径 
    47.   * 
    48.   * @param delpath 
    49.   *            String 
    50.   * @throws FileNotFoundException 
    51.   * @throws IOException 
    52.   * @return boolean 
    53.   */  
    54.  public static boolean readfile(String filepath)  
    55.    throws FileNotFoundException, IOException {  
    56.   try {  
    57.   
    58.    File file = new File(filepath);  
    59.    System.out.println("遍历的路径为:" + file.getAbsolutePath());  
    60.    // 当且仅当此抽象路径名表示的文件存在且 是一个目录时(即文件夹下有子文件时),返回 true  
    61.    if (!file.isDirectory()) {  
    62.     System.out.println("该文件的绝对路径:" + file.getAbsolutePath());  
    63.     System.out.println("名称:" + file.getName());  
    64.    } else if (file.isDirectory()) {  
    65.     // 得到目录中的文件和目录  
    66.     String[] filelist = file.list();  
    67.     if (filelist.length == 0) {  
    68.      System.out.println(file.getAbsolutePath()  
    69.        + "文件夹下,没有子文件夹或文件");  
    70.     } else {  
    71.      System.out  
    72.        .println(file.getAbsolutePath() + "文件夹下,有子文件夹或文件");  
    73.     }  
    74.     for (int i = 0; i < filelist.length; i++) {  
    75.      File readfile = new File(filepath + "\\" + filelist[i]);  
    76.      System.out.println("遍历的路径为:" + readfile.getAbsolutePath());  
    77.      if (!readfile.isDirectory()) {  
    78.       System.out.println("该文件的路径:"  
    79.         + readfile.getAbsolutePath());  
    80.       System.out.println("名称:" + readfile.getName());  
    81.      } else if (readfile.isDirectory()) {  
    82.       System.out.println("-----------递归循环-----------");  
    83.       readfile(filepath + "\\" + filelist[i]);  
    84.      }  
    85.     }  
    86.   
    87.    }  
    88.   
    89.   } catch (FileNotFoundException e) {  
    90.    System.out.println("readfile() Exception:" + e.getMessage());  
    91.   }  
    92.   return true;  
    93.  }  
    94.   
    95.  public static void main(String[] args) {  
    96.   try {  
    97.    // readfile("D:/file");  
    98.    deletefile("D:/file");  
    99.   } catch (Exception ex) {  
    100.    ex.printStackTrace();  
    101.   }  
    102.   System.out.println("ok");  
    103.  }  
    104.   
  • 相关阅读:
    Hibernate Validator
    RocketMQ之八:重试队列,死信队列,消息轨迹
    使用hibernate validator出现
    Hibernate Validator--创建自己的约束规则
    Java应用中使用ShutdownHook友好地清理现场、退出JVM的2种方法
    笔者带你剖析轻量级Sharding中间件——Kratos1.x
    [caffe]深度学习之图像分类模型VGG解读
    类的载入机制
    机器人api(图灵机器人)
    回文串问题总结
  • 原文地址:https://www.cnblogs.com/lraa/p/3090880.html
Copyright © 2011-2022 走看看