removeSVN

package test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public class FileManagerDeleteSvn 
{
	/**
	 * main方法
	 */
	public static void main(String[] args){
		new FileManagerDeleteSvn().removeSvnFilesExample();
	}
	
	/**
	 * 例子--==-- 删除指定文件夹下的所有svn相关文件
	 */
	public void removeSvnFilesExample(){
		//目标文件流
		BufferedWriter utput = null;
		//要移除SVN文件位置
//		String removeDir = "F:/temp/qhdm";
		String removeDir = "C:/Users/Beck/Desktop/arcgis/project/40/apache-tomcat-6.0.35-windows-x86/tomcat_Port8080_Send2AW/webapps/portal";
		//日志文件
		String logPath = "F:/temp/removeFileLog_"+Tools.formatDate(new Date(), "yyyy-MM-dd_HH-mm-ss")+".txt";
		try {
			//创建日志文件
			try {
				this.createFileAndAutoCreateDirectory(logPath);
				utput = new BufferedWriter(new FileWriter(new File(logPath)));
			} catch (Exception e) {
				e.printStackTrace();
			}
			//
			utput.write("删除指定文件夹下的所有svn相关文件---===---开始时间:"+Tools.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"+"\r\n"));
			//删除指定文件夹下的所有svn相关文件
			new FileManagerDeleteSvn().removeSvnFiles(removeDir,utput);
			//
			utput.write("删除指定文件夹下的所有svn相关文件---===---结束时间:"+Tools.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"+"\r\n"));
			utput.write("删除结束"+"\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try { utput.flush(); } catch (IOException e) { e.printStackTrace(); }
			try { utput.close(); } catch (IOException e) { e.printStackTrace(); }
			utput = null;
			System.out.println("删除指定文件夹下的所有svn相关文件---===---结束");
			System.out.println("日志文件:"+logPath);
		}
	}
	
	/**
	 * 删除指定文件夹下的所有svn相关文件
	 * @param filePath
	 */
	public void removeSvnFiles(String filePath, BufferedWriter utput) {
		try {
			File file = new File(filePath);
			//存在且为文件夹
			if (file.exists() && file.isDirectory()) {
				String[] files = file.list();
				for( int i=0; files!=null && i<files.length; i++ ){
					if(".svn".equals(files[i])){
						utput.write("-----=== 开始 "+filePath+"/.svn 文件夹删除 ===-----"+"\r\n");
						System.out.println("-----=== 开始 "+filePath+"/.svn 文件夹删除 ===-----");
						this.removeDirectoryAndFile(filePath+"/.svn", utput);
						System.out.println("-----=== 结束  "+filePath+"/.svn 文件夹删除 ===-----");
						utput.write("-----=== 结束 "+filePath+"/.svn 文件夹删除 ===-----"+"\r\n");
					} else {
						//递归
						this.removeSvnFiles(filePath+"/"+files[i], utput);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除指定文件或文件夹(如果是文件夹,删除其下的所有文件和文件夹,最后删除该文件夹)
	 * @param filePath
	 */
	public void removeDirectoryAndFile(String filePath, BufferedWriter utput) {
		try {
			File file = new File(filePath);
			boolean isDel = false;
			if( file.isFile() ){
				isDel = file.delete();
				utput.write("delete file "+isDel+" ===== "+filePath+"\r\n");
				System.out.println("delete file "+isDel+" ===== "+filePath);
			} else {
				String[] files = file.list();
				if( files!=null && files.length>0 ){
					//递归
					for(int i=0; i<files.length; i++){
						this.removeDirectoryAndFile(filePath+"/"+files[i], utput);
					}
					isDel = file.delete();
					utput.write("delete directory "+isDel+" ===== "+filePath+"\r\n");
					System.out.println("delete directory "+isDel+" ===== "+filePath);
				} else {
					isDel = file.delete();
					utput.write("delete directory "+isDel+" ===== "+filePath+"\r\n");
					System.out.println("delete directory "+isDel+" ===== "+filePath);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建文件,存在则不创建,文件夹不存在则自动创建
	 * @param filePath
	 */
	public void createFileAndAutoCreateDirectory(String filePath){
		try{
			File file = new File(filePath);//要创建的文件
			File parentDir = new File(file.getParent());//要创建的文件所在的文件夹
			if(!parentDir.exists()){ parentDir.mkdirs(); }//文件夹不存在则创建
			if (!file.exists()) { file.createNewFile(); }//文件不存在则创建
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}



package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Tools 
{
	/**
	 * 
	 * @param date  //new Date()
	 * @param format  //yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String formatDate(Date date, String format){
		String result = null;
		try{
			DateFormat dateFormat = new SimpleDateFormat(format);
			result = dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result; 
	}
}





你可能感兴趣的:(SVN)