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

          主站蜘蛛池模板: 邯郸市| 祥云县| 赣榆县| 玛多县| 汕尾市| 固阳县| 鄄城县| 横山县| 中方县| 神木县| 琼结县| 探索| 姜堰市| 连城县| 临颍县| 神木县| 天全县| 岑溪市| 甘洛县| 汶上县| 双鸭山市| 乌拉特中旗| 闵行区| 中西区| 石嘴山市| 河北省| 西贡区| 吉水县| 康保县| 鹤壁市| 南昌县| 江安县| 海宁市| 博罗县| 格尔木市| 房山区| 江都市| 如皋市| 汶上县| 德安县| 叙永县|