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

          主站蜘蛛池模板: 彰化县| 商洛市| 定州市| 洛浦县| 景东| 萨迦县| 石渠县| 香格里拉县| 炎陵县| 五指山市| 同江市| 齐河县| 广安市| 皋兰县| 时尚| 鄂尔多斯市| 湖北省| 达孜县| 临夏县| 赞皇县| 庆阳市| 醴陵市| 朝阳县| 五大连池市| 额尔古纳市| 锡林浩特市| 莫力| 石阡县| 松阳县| 宜良县| 益阳市| 临城县| 阳新县| 台北市| 武平县| 都江堰市| 昌吉市| 通州市| 仁化县| 喀什市| 大兴区|