Java删除svn文件

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

  1. package com.ycl.filter.FileCleaner;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.util.ArrayList;   
  6. import java.util.Collection;   
  7. import java.util.List;   
  8.   
  9. import org.apache.commons.io.DirectoryWalker;   
  10.   
  11. public class FileCleaner extends DirectoryWalker {   
  12.   
  13.     public FileCleaner() {   
  14.         super();   
  15.     }   
  16.   
  17.     public List<File> clean(File startDirectory) throws IOException {   
  18.         List<File> results = new ArrayList<File>();   
  19.         walk(startDirectory, results);   
  20.         return results;   
  21.     }   
  22.   
  23.     @Override  
  24.     protected void handleStart(File startDirectory, Collection results)   
  25.             throws IOException {   
  26.         System.out.println("-------开始清除-------");   
  27.     }   
  28.   
  29.     @Override  
  30.     protected void handleEnd(Collection results) throws IOException {   
  31.         System.out.println("-------结束清除-------");   
  32.     }   
  33.   
  34.     @Override  
  35.     protected void handleCancelled(File startDirectory, Collection results,   
  36.             CancelException cancel) throws IOException {   
  37.         System.out.println("-------清除异常-------");   
  38.         super.handleCancelled(startDirectory, results, cancel);   
  39.     }   
  40.   
  41.     @Override  
  42.     protected boolean handleIsCancelled(File file, int depth, Collection results)   
  43.             throws IOException {   
  44.         //这里可以设置断点,比如当你找到某个类的时候,停止遍历,默认继续   
  45.         return false;   
  46.     }   
  47.   
  48.     @Override  
  49.     protected void handleDirectoryStart(File directory, int depth,   
  50.             Collection results) throws IOException {   
  51.     //  System.out.println("****开始处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());   
  52.     }   
  53.   
  54.     @Override  
  55.     protected void handleDirectoryEnd(File directory, int depth,   
  56.             Collection results) throws IOException {   
  57.     //  System.out.println("****结束处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());   
  58.     }   
  59.   
  60.     @Override  
  61.     protected void handleRestricted(File directory, int depth,   
  62.             Collection results) throws IOException {   
  63.         System.out.println("****受限制目录:"+directory.getName()+"deep:"+depth+"results:"+results.toString());   
  64.     }   
  65.   
  66.     /**  
  67.      * 是否处理某个目录.返回false 不处理  
  68.      *  
  69.      * @see 这里直接删除.svn.然后不处理.  
  70.      */  
  71.     @Override  
  72.     protected boolean handleDirectory(File directory, int depth,   
  73.             Collection results) {   
  74.         // delete svn directories and then skip   
  75.         if (".svn".equals(directory.getName())) {   
  76.             deleteDirectory(directory,results);   
  77.             return false;   
  78.         } else {   
  79.             results.add(directory);//删除.svn,还有哪些文件夹   
  80.             return true;   
  81.         }   
  82.   
  83.     }   
  84.   
  85.     /**  
  86.      * 删除文件,并把文件加到删除列表中  
  87.      */  
  88.     @Override  
  89.     protected void handleFile(File file, int depth, Collection results) {   
  90.         // delete file and add to list of deleted   
  91.         //file.delete();   
  92.         //results.add(file);   
  93.         //删除.svn文件后,还有哪些文件   
  94.     }   
  95.   
  96.     /**  
  97.      * 删除目录及目录下的文件夹和文件  
  98.      * @param directory  
  99.      * @param results  
  100.      */  
  101.     private void deleteDirectory(File directory,Collection results){   
  102.         if(directory.isDirectory()){   
  103.             File[] list = directory.listFiles();   
  104.             for(File file:list){   
  105.                 deleteDirectory(file,results);   
  106.             }   
  107.         }   
  108.         Log(directory.delete(),directory);   
  109.         results.add(directory);//删除文件   
  110.     }   
  111.   
  112.     /**  
  113.      * 删除文件或者目录失败日志  
  114.      * @param flag  
  115.      */  
  116.     private void Log(boolean flag,File directory){   
  117.         if(!flag){   
  118.             System.err.println("删除文件失败:"+directory.getAbsolutePath());   
  119.         }else{   
  120.             System.out.println("delete:"+directory.getAbsolutePath());   
  121.         }   
  122.     }   
  123. }  
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("-------结束清除-------");
	}

	@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 {
		//这里可以设置断点,比如当你找到某个类的时候,停止遍历,默认继续
		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("****结束处理:"+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());
	}

	/**
	 * 是否处理某个目录.返回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);//删除文件
	}

	/**
	 * 删除文件或者目录失败日志
	 * @param flag
	 */
	private void Log(boolean flag,File directory){
		if(!flag){
			System.err.println("删除文件失败:"+directory.getAbsolutePath());
		}else{
			System.out.println("delete:"+directory.getAbsolutePath());
		}
	}
}


测试代码如下:

Java代码 复制代码  收藏代码
  1. package com.ycl.filter.FileCleaner;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.util.List;   
  6.   
  7. public class TestFileCleaner {   
  8.   
  9.     /**  
  10.      * @param args  
  11.      * @throws IOException  
  12.      */  
  13.     public static void main(String[] args) throws IOException {   
  14.         // TODO Auto-generated method stub   
  15.         FileCleaner cleaner = new FileCleaner();   
  16.         File startDirectory = new File("D://workspace//branches//pamirsshop_branches_8-30");   
  17.         List<File> list = cleaner.clean(startDirectory);   
  18.         for(File file:list){   
  19.         //  System.out.println("file:"+file.getAbsolutePath());   
  20.         }   
  21.         System.out.println("共处理"+list.size()+"个文件");   
  22.   
  23.     }   
  24. }  
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()+"个文件");

	}
}

 

  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. -------结束清除-------   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值