天天看點

Java删除svn檔案

現在很多公司采用SVN開發,其版本管理的确很不錯.

但是有一點很讓人郁悶,就是源代碼的.svn檔案會很多,而且當Java源碼代或者配置檔案改變多次時,會生成很多版本,svn的大小可能是源代碼的N倍.

如果想把某個目錄的svn檔案去掉,可以自己寫個程式去删除.svn目錄下的所有檔案.友善又時用,我這裡采用的是commons-io.jar 裡的 DirectoryWalker,看到名稱就能了解“目錄散步”。

廢話不說了,代碼如下:

  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.     @Override  
  54.     protected boolean handleDirectory(File directory, int depth,   
  55.             Collection results) {   
  56.         // delete svn directories and then skip   
  57.         if (".svn".equals(directory.getName())) {   
  58.             deleteDirectory(directory,results);   
  59.             return false;   
  60.         } else {   
  61.             results.add(directory);//删除.svn,還有哪些檔案夾   
  62.             return true;   
  63.         }   
  64.     }   
  65.     @Override  
  66.     protected void handleFile(File file, int depth, Collection results) {   
  67.         // delete file and add to list of deleted   
  68.         //file.delete();   
  69.         //results.add(file);   
  70.         //删除.svn檔案後,還有哪些檔案   
  71.     }   
  72.     private void deleteDirectory(File directory,Collection results){   
  73.         if(directory.isDirectory()){   
  74.             File[] list = directory.listFiles();   
  75.             for(File file:list){   
  76.                 deleteDirectory(file,results);   
  77.             }   
  78.         }   
  79.         Log(directory.delete(),directory);   
  80.         results.add(directory);//删除檔案   
  81.     }   
  82.     private void Log(boolean flag,File directory){   
  83.         if(!flag){   
  84.             System.err.println("删除檔案失敗:"+directory.getAbsolutePath());   
  85.         }else{   
  86.             System.out.println("delete:"+directory.getAbsolutePath());   
  87.         }   
  88.     }   
  89. }  
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代碼

Java删除svn檔案
Java删除svn檔案
Java删除svn檔案
  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.     public static void main(String[] args) throws IOException {   
  7.         // TODO Auto-generated method stub   
  8.         FileCleaner cleaner = new FileCleaner();   
  9.         File startDirectory = new File("D://workspace//branches//pamirsshop_branches_8-30");   
  10.         List<File> list = cleaner.clean(startDirectory);   
  11.         for(File file:list){   
  12.         //  System.out.println("file:"+file.getAbsolutePath());   
  13.         }   
  14.         System.out.println("共處理"+list.size()+"個檔案");   
  15.     }   
  16. }  
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. -------結束清除-------