packagecom.covics.zfh;importjava.io.File;importjava.io.FilenameFilter;/*** 实现带*号的删除文件
*@authordoudou
**/
public classTestDeleteFile {public static voidmain(String args[]) {boolean delFile =
//windows//deleteFileWithSign("E:\\fileDeleteTest\\test\\t*ve.txt");//linux
deleteFileWithSign("/home/work/ssst*ve.txt");
System.out.println("main---->"+delFile);
}/*** 删除带*的文件
*@paramfileName
*@return
*/
public static booleandeleteFileWithSign(String fileName) {//文件夹名称
String dirName = "";//要删除的文件类型
String fileType = "";//不带后缀的文件名
String nameWithoutType = "";//*号个数
int num = 0;if (null == fileName || "".equals(fileName)) {return false;
}
String osName= System.getProperty("os.name")if (osName.toLowerCase().contains("windows")){int index = fileName.lastIndexOf("\\") + 1;
}else{int index = fileName.lastIndexOf("/") + 1;
}
nameWithoutType= fileName.substring(index, fileName.lastIndexOf("."));
fileType=fileName
.substring(fileName.lastIndexOf("."), fileName.length());
dirName= fileName.substring(0, index);
File dir= newFile(dirName);
System.out.println("dir:"+dir);
System.out.println("exits"+dir.exists());
System.out.println("dir.isDirectory()"+dir.isDirectory());if (!dir.exists() || !dir.isDirectory()) {return false;
}//*号的位置
int start = -1, end = -1;
start= nameWithoutType.indexOf("*");
end= nameWithoutType.lastIndexOf("*");int length =nameWithoutType.length();for (int i=0;i
num++;
}
}
System.out.println("num---->"+num);//删除的类型
int delType = 0;switch(num) {case 0:returndeleteFile(fileName);case 1 : //只有一个*号的状况
if (0==start) { //在开始的位置
delType = 1; //只调用endWith方法
} else if (length-1 == start) { //在结束的位置
delType = 2; //只调用startWith方法
} else { //在中间
delType = 3; //只调用contain方法
}break;default : //有两个或多个*号的状况
if (0 == start && length-1 == end) { //在开始的位置和结束的位置都有*号
delType = 4; //只调用contain方法
} else if (0 == start && length-1 != end) { //开始的位置有*号且结束的位置没有*号
delType = 5; //调用contain和endWith方法
} else if (0 != start && length-1 == end) { //开始的位置没有*号且结束的位置有*号
delType = 6; //调用startWith和contain方法
} else { //*号在中间 的状况(开始和结束都没有*号)
delType = 7; //只调用contain方法
}break;
}boolean flag = false;
String[] names=dir
.list(newDelFileFilter(nameWithoutType, fileType, delType));
System.out.println("names.length---->"+names.length);for (int i = 0; i < names.length; i++) {
System.out.println("f--names-->"+dirName+names[i]);
File f= new File(dirName +names[i]);if (!f.exists() || !f.isFile()) {return false;
}
flag=deleteFile(f.getAbsolutePath());if (!flag) {break;
}
}returnflag;
}/*** 删除单个文件
*
*@paramfileName
*@return
*/
public static booleandeleteFile(String fileName) {
File file= newFile(fileName);if (file.isFile() &&file.exists()) {
file.delete();return true;
}return false;
}/*** 删除文件夹
*
*@paramdir
*@return
*/
public static booleandeleteDir(String dir) {//判断是否有带文件夹符号。
if (!dir.endsWith(File.separator)) {
dir= dir +File.separator;
}
File file= newFile(dir);if (!file.isDirectory() || !file.exists()) {return false;
}boolean flag = true;
File[] files=file.listFiles();for (int i = 0; i < files.length; i++) {if (files[i].isFile()) { //删除文件
flag =deleteFile(files[i].getAbsolutePath());if (!flag) {break;
}
}else if (files[i].isDirectory()) { //删除文件夹
flag =deleteDir(files[i].getAbsolutePath());if (!flag) {break;
}
}else{return false;
}
}if (file.delete()) { //删除自身
return true;
}return false;
}
}/*** FileFilter类
* 删除带*文件
*@authordoudou
**/
class DelFileFilter implementsFilenameFilter {//传进来的文件URL
String fileName;
String fileType;//文件类型//删除的类型。 1:*号在开头; 2:*号在末尾
intdelType;public DelFileFilter(String name, String fileType, intdelType) {this.fileType =fileType;this.delType =delType;this.fileName =name;
}
@Overridepublic booleanaccept(File dir, String name) {//将文件名分割顾字符串
String[] ss = fileName.split("[*]");//首先判断是不是要删除的文件类型
if (!isThisTypeFile(name)) {return false;
}boolean flag = true;switch(delType) {case 1:for(String s : ss) {if (!"".equals(s)){
flag=endWithS(name, s);
}
}returnflag;case 2:for(String s : ss) {if (!"".equals(s)){
flag=startWithS(name, s);
}
}returnflag;case 3:case 4:case 7:for(String s : ss) {if (!"".equals(s)) {
flag=contains(name, s);if (!flag) {break;
}
}
}returnflag;case 5:boolean flag1 = false, flag2 = false;for (int i=0;i
flag1=contains(name, ss[i]);if (!flag1) {break;
}
}if (i == ss.length-1) {
flag2=endWithS(name, ss[i]);
}
}
}
flag= flag1 &&flag2;returnflag;case 6:boolean flag3 = false, flag4 = false;
flag3= startWithS(name, ss[0]);for (int i=1;i
flag4=contains(name, ss[i]);if (!flag4)break;
}
}
flag= flag3 &&flag4;returnflag;default:
flag= false;returnflag;
}
}/*** 判断是不是该类型的文件
*@paramfile
*@return
*/
public booleanisThisTypeFile(String file) {returnfile.toLowerCase().endsWith(fileType);
}/*** 包含某字符
*@paramfile
*@paramstr
*@return
*/
public booleancontains(String file, String str) {return(file.toLowerCase().contains(str));
}/*** 以某字符开始
*@paramfile
*@paramstr
*@return
*/
public booleanstartWithS(String file, String str) {returnfile.toLowerCase().startsWith(str);
}/*** 以file字符结束
*@paramfile
*@paramstr
*@return
*/
public booleanendWithS(String file, String str) {return file.toLowerCase().endsWith(str+fileType);
}
}