Java 刪除文件夾里所有文件

          Posted on 2008-08-25 18:30 E.ven 閱讀(1446) 評論(1)  編輯  收藏
          import java.io.File;

          public class Test
          {
             
          public static void main(String args[]){
                 Test t 
          = new Test();
                 delFolder(
          "c:/bb");
                 System.out.println(
          "deleted");
          }


          //刪除文件夾
          //param folderPath 文件夾完整絕對路徑

              
          public static void delFolder(String folderPath) {
               
          try {
                  delAllFile(folderPath); 
          //刪除完里面所有內容
                  String filePath = folderPath;
                  filePath 
          = filePath.toString();
                  java.io.File myFilePath 
          = new java.io.File(filePath);
                  myFilePath.delete(); 
          //刪除空文件夾
               }
           catch (Exception e) {
                 e.printStackTrace(); 
               }

          }


          //刪除指定文件夾下所有文件
          //param path 文件夾完整絕對路徑
             
          public static boolean delAllFile(String path) {
                 
          boolean flag = false;
                 File file 
          = new File(path);
                 
          if (!file.exists()) {
                   
          return flag;
                 }

                 
          if (!file.isDirectory()) {
                   
          return flag;
                 }

                 String[] tempList 
          = file.list();
                 File temp 
          = null;
                 
          for (int i = 0; i < tempList.length; i++{
                    
          if (path.endsWith(File.separator)) {
                       temp 
          = new File(path + tempList[i]);
                    }
           else {
                        temp 
          = new File(path + File.separator + tempList[i]);
                    }

                    
          if (temp.isFile()) {
                       temp.delete();
                    }

                    
          if (temp.isDirectory()) {
                       delAllFile(path 
          + "/" + tempList[i]);//先刪除文件夾里面的文件
                       delFolder(path + "/" + tempList[i]);//再刪除空文件夾
                       flag = true;
                    }

                 }

                 
          return flag;
               }

          }

          Feedback

          # re: Java 刪除文件夾里所有文件  回復  更多評論   

          2008-12-15 12:02 by sdf
          不錯..不錯..

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 27, comments - 15, trackbacks - 0, articles - 1

          Copyright © E.ven

          主站蜘蛛池模板: 扶绥县| 申扎县| 方山县| 巴彦淖尔市| 犍为县| 石狮市| 麻栗坡县| 响水县| 咸丰县| 大城县| 枣庄市| 荔波县| 白河县| 肇东市| 苍山县| 京山县| 湄潭县| 夹江县| 平昌县| 安陆市| 化州市| 栾川县| 茌平县| 连平县| 普陀区| 卓资县| 宜兰县| 津南区| 米易县| 铜鼓县| 孟连| 牟定县| 灌云县| 珠海市| 丰宁| 阜新市| 五莲县| 广汉市| 托克逊县| 上蔡县| 宜良县|