用java的IO删除zip文件失败,是何道理?

用java的io生成的zip文件删除不掉,同一目录的csv能删除,以至于这个目录不能删除。在生成完文件后,相关的流也close了。

我这里还有个下载这个zip的操作,同样把response.getOutputStream().close(),应该不存在权限的问题。下面是删除的方法:

public void delAllFile(String path) {  
        File file = new File(path);  
        if (!file.exists()) {  
          return;  
        }  
        if (!file.isDirectory()) {  
          return;  
        }  
        String[] tempList = file.list();  
        File temp = null;  
        for (int i = 0; i < tempList.length; i++) {  
          if (path.endsWith(File.separator)) {  
            temp = new File(path + tempList[i]);  
          }  
          else {  
            temp = new File(path + File.separator + tempList[i]);  
          }  
          if (temp.isFile()) {  
            temp.delete();  
          }
//            if (temp.isDirectory()) {  
//              delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件  
//              delFolder(path+"/"+ tempList[i]);//再删除空文件夹  
//            }  
        }
        file.delete();
    }  

我觉得问题还是zip文件被操作系统占用吧,再检查一下对这些zip文件的引用,找找没有关闭的流。

删除前 调用 System.gc();试试

[url]http://www.iteye.com/topic/180807[/url]

[code="java"] /** //*递归删除文件夹
* @param path
*/
public void delDir(String path){
File dir=new File(path);
if(dir.exists()){
File[] tmp=dir.listFiles();
for(int i=0;i<tmp.length;i++){
if(tmp[i].isDirectory()){
delDir(path+"/"+tmp[i].getName());
}
else{
tmp[i].delete();
}
}
dir.delete();
}
}
[/code]

建议你用org.apache.commons.io.FileUtils.deleteDirectory(File file)去删除目录、

我也遇到了这种问题,不能删除zip压缩包,
代码如下
File file=new File(url);
File[]fileList=file.listFiles();
for(File newFile:fileList){
newFile.delete();
}
file.delete();