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;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值