posts - 241,  comments - 116,  trackbacks - 0
          現(xiàn)在很多公司采用SVN開發(fā),其版本管理的確很不錯(cuò).
          但是有一點(diǎn)很讓人郁悶,就是源代碼的.svn文件會(huì)很多,而且當(dāng)Java源碼代或者配置文件改變多次時(shí),會(huì)生成很多版本,svn的大小可能是源代碼的N倍.
          如果想把某個(gè)目錄的svn文件去掉,可以自己寫個(gè)程序去刪除.svn目錄下的所有文件.方便又時(shí)用,我這里采用的是commons-io.jar 里的 DirectoryWalker,看到名稱就能理解“目錄散步”。
          廢話不說了,代碼如下:
          package com.ycl.filter.FileCleaner;

          import java.io.File;
          import java.io.IOException;
          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.List;

          import org.apache.commons.io.DirectoryWalker;

          public class FileCleaner extends DirectoryWalker {

              public FileCleaner() {
                  super();
              }

              public List<File> clean(File startDirectory) throws IOException {
                  List<File> results = new ArrayList<File>();
                  walk(startDirectory, results);
                  return results;
              }

              @Override
              protected void handleStart(File startDirectory, Collection results)
                      throws IOException {
                  System.out.println("-------開始清除-------");
              }

              @Override
              protected void handleEnd(Collection results) throws IOException {
                  System.out.println("-------結(jié)束清除-------");
              }

              @Override
              protected void handleCancelled(File startDirectory, Collection results,
                      CancelException cancel) throws IOException {
                  System.out.println("-------清除異常-------");
                  super.handleCancelled(startDirectory, results, cancel);
              }

              @Override
              protected boolean handleIsCancelled(File file, int depth, Collection results)
                      throws IOException {
                  //這里可以設(shè)置斷點(diǎn),比如當(dāng)你找到某個(gè)類的時(shí)候,停止遍歷,默認(rèn)繼續(xù)
                  return false;
              }

              @Override
              protected void handleDirectoryStart(File directory, int depth,
                      Collection results) throws IOException {
              //    System.out.println("****開始處理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
              }

              @Override
              protected void handleDirectoryEnd(File directory, int depth,
                      Collection results) throws IOException {
              //    System.out.println("****結(jié)束處理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
              }

              @Override
              protected void handleRestricted(File directory, int depth,
                      Collection results) throws IOException {
                  System.out.println("****受限制目錄:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
              }

              /**
               * 是否處理某個(gè)目錄.返回false 不處理
               *
               * @see 這里直接刪除.svn.然后不處理.
               */
              @Override
              protected boolean handleDirectory(File directory, int depth,
                      Collection results) {
                  // delete svn directories and then skip
                  if (".svn".equals(directory.getName())) {
                      deleteDirectory(directory,results);
                      return false;
                  } else {
                      results.add(directory);//刪除.svn,還有哪些文件夾
                      return true;
                  }

              }

              /**
               * 刪除文件,并把文件加到刪除列表中
               */
              @Override
              protected void handleFile(File file, int depth, Collection results) {
                  // delete file and add to list of deleted
                  //file.delete();
                  //results.add(file);
                  //刪除.svn文件后,還有哪些文件
              }

              /**
               * 刪除目錄及目錄下的文件夾和文件
               * @param directory
               * @param results
               */
              private void deleteDirectory(File directory,Collection results){
                  if(directory.isDirectory()){
                      File[] list = directory.listFiles();
                      for(File file:list){
                          deleteDirectory(file,results);
                      }
                  }
                  Log(directory.delete(),directory);
                  results.add(directory);//刪除文件
              }

              /**Tp-Link教程
               * 刪除文件或者目錄失敗日志
               * @param flag
               */
              private void Log(boolean flag,File directory){
                  if(!flag){
                      System.err.println("刪除文件失敗:"+directory.getAbsolutePath());
                  }else{
                      System.out.println("delete:"+directory.getAbsolutePath());
                  }
              }iphone5
          }
          測(cè)試代碼如下:
          package com.ycl.filter.FileCleaner;

          import java.io.File;
          import java.io.IOException;
          import java.util.List;

          public class TestFileCleaner {

              /**
               * @param args
               * @throws IOException
               */
              public static void main(String[] args) throws IOException {
                  // TODO Auto-generated method stub
                  FileCleaner cleaner = new FileCleaner();
                  File startDirectory = new File("D:\\workspace\\branches\\pamirsshop_branches_8-30");
                  List<File> list = cleaner.clean(startDirectory);
                  for(File file:list){
                  //    System.out.println("file:"+file.getAbsolutePath());
                  }
                  System.out.println("共處理"+list.size()+"個(gè)文件");

              }
          }
          posted on 2011-06-21 10:24 墻頭草 閱讀(232) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運(yùn)專家
          主站蜘蛛池模板: 峨眉山市| 建昌县| 建平县| 塔城市| 房山区| 隆子县| 股票| 额济纳旗| 开鲁县| 嘉定区| 岱山县| 阜新市| 望江县| 伊金霍洛旗| 张家港市| 巴彦淖尔市| 蒙山县| 碌曲县| 静宁县| 宣汉县| 镇平县| 北票市| 调兵山市| 元朗区| 清新县| 聊城市| 托克逊县| 文成县| 宜良县| 江都市| 仁寿县| 岢岚县| 抚远县| 新乡县| 陆川县| 仁布县| 仲巴县| 汉阴县| 民丰县| 合作市| 城口县|