import
java.io.File;
public class Test
{
publicstaticvoidmain(Stringargs[]){
Testt=newTest();
delFolder("c:/bb");
System.out.println("deleted");
}
//删除文件夹
//paramfolderPath文件夹完整绝对路径
publicstaticvoiddelFolder(StringfolderPath){
try{
delAllFile(folderPath);//删除完里面所有内容
StringfilePath=folderPath;
filePath=filePath.toString();
java.io.FilemyFilePath=newjava.io.File(filePath);
myFilePath.delete();//删除空文件夹
}catch(Exceptione){
e.printStackTrace();
}
}
//删除指定文件夹下所有文件
//parampath文件夹完整绝对路径
publicstaticbooleandelAllFile(Stringpath){
booleanflag=false;
Filefile=newFile(path);
if(!file.exists()){
returnflag;
}
if(!file.isDirectory()){
returnflag;
}
String[]tempList=file.list();
Filetemp=null;
for(inti=0;i<tempList.length;i++){
if(path.endsWith(File.separator)){
temp=newFile(path+tempList[i]);
}else{
temp=newFile(path+File.separator+tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if(temp.isDirectory()){
delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+tempList[i]);//再删除空文件夹
flag=true;
}
}
returnflag;
}
}
public class Test
{
publicstaticvoidmain(Stringargs[]){
Testt=newTest();
delFolder("c:/bb");
System.out.println("deleted");
}
//删除文件夹
//paramfolderPath文件夹完整绝对路径
publicstaticvoiddelFolder(StringfolderPath){
try{
delAllFile(folderPath);//删除完里面所有内容
StringfilePath=folderPath;
filePath=filePath.toString();
java.io.FilemyFilePath=newjava.io.File(filePath);
myFilePath.delete();//删除空文件夹
}catch(Exceptione){
e.printStackTrace();
}
}
//删除指定文件夹下所有文件
//parampath文件夹完整绝对路径
publicstaticbooleandelAllFile(Stringpath){
booleanflag=false;
Filefile=newFile(path);
if(!file.exists()){
returnflag;
}
if(!file.isDirectory()){
returnflag;
}
String[]tempList=file.list();
Filetemp=null;
for(inti=0;i<tempList.length;i++){
if(path.endsWith(File.separator)){
temp=newFile(path+tempList[i]);
}else{
temp=newFile(path+File.separator+tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if(temp.isDirectory()){
delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
delFolder(path+"/"+tempList[i]);//再删除空文件夹
flag=true;
}
}
returnflag;
}
}