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

          主站蜘蛛池模板: 区。| 海门市| 崇礼县| 黄陵县| 沁水县| 鄢陵县| 新兴县| 石台县| 樟树市| 兴城市| 福泉市| 泸州市| 韶山市| 庐江县| 东辽县| 醴陵市| 连江县| 安多县| 和龙市| 壤塘县| 太原市| 德安县| 三亚市| 蓬溪县| 岐山县| 洪洞县| 友谊县| 怀柔区| 肇州县| 葫芦岛市| 古丈县| 天峨县| 台前县| 交口县| 肃北| 来宾市| 益阳市| 阿鲁科尔沁旗| 黔西县| 邳州市| 南投县|