免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2975 | 回复: 0
打印 上一主题 下一主题

Java删除svn文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-21 17:35 |只看该作者 |倒序浏览
Java删除svn文件




现在很多公司采用SVN开发,其版本管理的确很不错.
但是有一点很让人郁闷,就是源代码的.svn文件会很多,而且当Java源码代或者配置文件改变多次时,会生成很多版本,svn的大小可能是源代码的N倍.
如果想把某个目录的svn文件去掉,可以自己写个程序去删除.svn目录下的所有文件.方便又时用,我这里采用的是commons-io.jar 里的 DirectoryWalker,看到名称就能理解“目录**”。
废话不说了,代码如下:

Java代码
  1. package com.ycl.filter.FileCleaner;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.List;

  7. import org.apache.commons.io.DirectoryWalker;

  8. public class FileCleaner extends DirectoryWalker {

  9.         public FileCleaner() {
  10.                 super();
  11.         }

  12.         public List<File> clean(File startDirectory) throws IOException {
  13.                 List<File> results = new ArrayList<File>();
  14.                 walk(startDirectory, results);
  15.                 return results;
  16.         }

  17.         @Override
  18.         protected void handleStart(File startDirectory, Collection results)
  19.                         throws IOException {
  20.                 System.out.println("-------开始清除-------");
  21.         }

  22.         @Override
  23.         protected void handleEnd(Collection results) throws IOException {
  24.                 System.out.println("-------结束清除-------");
  25.         }

  26.         @Override
  27.         protected void handleCancelled(File startDirectory, Collection results,
  28.                         CancelException cancel) throws IOException {
  29.                 System.out.println("-------清除异常-------");
  30.                 super.handleCancelled(startDirectory, results, cancel);
  31.         }

  32.         @Override
  33.         protected boolean handleIsCancelled(File file, int depth, Collection results)
  34.                         throws IOException {
  35.                 //这里可以设置断点,比如当你找到某个类的时候,停止遍历,默认继续
  36.                 return false;
  37.         }

  38.         @Override
  39.         protected void handleDirectoryStart(File directory, int depth,
  40.                         Collection results) throws IOException {
  41.         //        System.out.println("****开始处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
  42.         }

  43.         @Override
  44.         protected void handleDirectoryEnd(File directory, int depth,
  45.                         Collection results) throws IOException {
  46.         //        System.out.println("****结束处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
  47.         }

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

  53.         /**
  54.          * 是否处理某个目录.返回false 不处理
  55.          *
  56.          * @see 这里直接删除.svn.然后不处理.
  57.          */
  58.         @Override
  59.         protected boolean handleDirectory(File directory, int depth,
  60.                         Collection results) {
  61.                 // delete svn directories and then skip
  62.                 if (".svn".equals(directory.getName())) {
  63.                         deleteDirectory(directory,results);
  64.                         return false;
  65.                 } else {
  66.                         results.add(directory);//删除.svn,还有哪些文件夹
  67.                         return true;
  68.                 }

  69.         }

  70.         /**
  71.          * 删除文件,并把文件加到删除列表中
  72.          */
  73.         @Override
  74.         protected void handleFile(File file, int depth, Collection results) {
  75.                 // delete file and add to list of deleted
  76.                 //file.delete();
  77.                 //results.add(file);
  78.                 //删除.svn文件后,还有哪些文件
  79.         }

  80.         /**
  81.          * 删除目录及目录下的文件夹和文件
  82.          * @param directory
  83.          * @param results
  84.          */
  85.         private void deleteDirectory(File directory,Collection results){
  86.                 if(directory.isDirectory()){
  87.                         File[] list = directory.listFiles();
  88.                         for(File file:list){
  89.                                 deleteDirectory(file,results);
  90.                         }
  91.                 }
  92.                 Log(directory.delete(),directory);
  93.                 results.add(directory);//删除文件
  94.         }

  95.         /**
  96.          * 删除文件或者目录失败日志
  97.          * @param flag
  98.          */
  99.         private void Log(boolean flag,File directory){
  100.                 if(!flag){
  101.                         System.err.println("删除文件失败:"+directory.getAbsolutePath());
  102.                 }else{
  103.                         System.out.println("delete:"+directory.getAbsolutePath());
  104.                 }
  105.         }
  106. }
复制代码
测试代码如下:

Java代码
  1. package com.ycl.filter.FileCleaner;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.List;

  5. public class TestFileCleaner {

  6.         /**
  7.          * @param args
  8.          * @throws IOException
  9.          */
  10.         public static void main(String[] args) throws IOException {
  11.                 // TODO Auto-generated method stub
  12.                 FileCleaner cleaner = new FileCleaner();
  13.                 File startDirectory = new File("D:\\workspace\\branches\\pamirsshop_branches_8-30");
  14.                 List<File> list = cleaner.clean(startDirectory);
  15.                 for(File file:list){
  16.                 //        System.out.println("file:"+file.getAbsolutePath());
  17.                 }
  18.                 System.out.println("共处理"+list.size()+"个文件");

  19.         }
  20. }
复制代码
测试结果如下:

Java代码
  1. ...
  2. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\.svn
  3. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\text-base\assembly.xml.svn-base
  4. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\text-base
  5. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\prop-base
  6. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\props
  7. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\tmp\text-base
  8. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\tmp\prop-base
  9. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\tmp\props
  10. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\tmp
  11. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\all-wcprops
  12. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn\entries
  13. delete:D:\workspace\branches\pamirsshop_branches_8-30\deploy\src\main\assembly\.svn
  14. -------结束清除-------
  15. 共处理3331个文件
复制代码
记得我刚碰到svn的这个问题的时候,也是到处找工具,有没有啥工具可以去掉.svn的,现在想想,自己是程序员,遇到问题,自己开发一个不就完了嘛,实用又方便.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP