项目要求,添加方案时在某一共享路径中要生成一个shape文件,当然在删除该方案时,也要删除这个shape文件。因为shape文件并不是一个,而是好多个文件放在文件夹内,所以需要递归删除,废话不多说,直接上代码了。
因为项目性能要求,删除方案时反应要快,但是删除共享路径中的shape文件大概需要2秒左右的样子,所以就放在了一个线程中。
public Class DeleteShpThread(){
String sPath;
public DeleteShpThread(String sPath){
this.sPath = sPath;
}
public void run(String sPath){
File file = new File(sPath);
if(file.isFile()){
deleteFile(sPath);
}else if(file.isDirectory()){
deleteDirectory(sPath);
}
}
public void deleteDirectory(String sPath){
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if(!sPath.endsWith(File.separator)){
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dirFile对应的文件存在,而且是一个目录
if(dirFile.exists() && dirFile.isDirectory){
File[] files = dirFile.listFiles();
for(int i = 0;i < files.length; i++){
//删除子文件
if(files[i].isFile){
deleteFile(files[i].getAbsolutePath());
}else{
deleteDirectory(files[i].getAbsolutePath());
}
//删除当前目录
dirFile.delete();
}
}
}
public void deleteFile(String sPath){
File file = new File(sPath);
//路径为文件且不为空
if(file.isFile() && file.exists()){
file.delete();
}
}
}