Java 刪除文件夾里所有文件

          Posted on 2008-08-25 18:30 E.ven 閱讀(1443) 評論(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

          主站蜘蛛池模板: 双鸭山市| 苏尼特左旗| 拉萨市| 阿瓦提县| 咸阳市| 平顺县| 苏尼特左旗| 临泉县| 利川市| 仙居县| 建瓯市| 彩票| 苏尼特左旗| 新营市| 斗六市| 莱阳市| 青浦区| 陕西省| 确山县| 集安市| 紫金县| 汝城县| 共和县| 上高县| 定西市| 福鼎市| 静宁县| 大化| 肇源县| 耒阳市| 永胜县| 天峻县| 辛集市| 灵寿县| 双鸭山市| 泗洪县| 法库县| 集安市| 鸡泽县| 曲松县| 上思县|