Java on Line

          和java的日子!

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            8 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks
          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;
               }

          }
          posted on 2007-10-12 16:19 陳東 閱讀(18619) 評論(1)  編輯  收藏 所屬分類: Java基礎

          評論

          # re: 用java刪除文件夾里的所有文件[未登錄] 2013-10-01 01:03 tao
          thanks.  回復  更多評論
            

          主站蜘蛛池模板: 嵊泗县| 江陵县| 烟台市| 广饶县| 正宁县| 九江县| 宾阳县| 夏邑县| 丹阳市| 新巴尔虎右旗| 澜沧| 古交市| 蒲城县| 砀山县| 大田县| 巴东县| 鲜城| 河池市| 西乌珠穆沁旗| 曲水县| 葫芦岛市| 石林| 沛县| 湖南省| 德钦县| 阳原县| 磐石市| 芜湖县| 马尔康县| 门源| 牡丹江市| 常宁市| 赤水市| 东阿县| 湘乡市| 淮安市| 贺州市| 陆丰市| 阿图什市| 大厂| 正阳县|