java中让人蛋疼的delete

项目中要删除文件夹, 只有一层, 下面有zip包, jpg图片, xml文件, 但是在删除时, 有一部分文件却删不掉, delete的结果是false:
public void deleteDir(File file)
	{
		if (file.exists())
		{

			File files[] = file.listFiles();
			for (int i = 0; i < files.length; i++)
			{
				System.out.println("删除" + files[i].getAbsolutePath());
				boolean b = files[i].delete();
				System.out.println(b);
			}

			file.delete();
		}
		else
		{
			System.out.println("所删除的文件不存在!" + '\n');
		}

	}

但是, 写一个个main方法, 传进去目录路径, 可以全部删除, 百思不得其解。

经过google之后, 很幸运, 找到了相同的遭遇者:

http://stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java


引用:

I would argue it's a bug then: java.sun.com/javase/6/docs/api/java/io/…() It says in there that the method should close the stream and any resources associated with it. I usually like to close all the streams in the inverted sequence (the last ones to be open are the first ones to be closed) using IOUtils.closeQuietly, but it tends to be overkill. 


Another bug in Java. I seldom find them, only my second in my 10 year career. This is my solution, as others have mentioned. I have nether used System.gc(). But here, in my case, it is absolutely crucial. Weird? YES!
finally
{
    try
    {
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        System.gc();
    }
    catch (IOException e)
    {
        logger.error(e.getMessage());
        e.printStackTrace();
    }
}
YEP! That was it for me too. I was using FileOutputStream. After setting out=null and then explicit call to System.gc() , it worked


然后, 我突然明白了, 赶紧测试, 果然搞定, 是这样的, 

我们的业务逻辑是, 根据业务号从数据库读取该业务号下影像的路径, 然后找到对应的文件并复制到以业务号命名的文件夹下, 以业务号命名的文件夹放在指定的文件夹destdir下, 然后然后根据影像信息生成xml报文, 

当报文生成后, 将影像和报文打包成zip, 然后将zip通过第三方接口上传到影像平台。当一个业务上传成功后, 要删除以这个业务号命名的目录, 就是在删除时出现了以上描述的问题, 那么到底是哪个环节的问题?


一个是拷贝文件, 一个是生成zip包, 无非这两个地方, 所以, 统统加了赋空和回收的操作:


	fos.flush();
			fos.close();
			fis.close();
			fis = null;
			fos = null;
			System.gc();

public boolean zipMaterial(String dir, String busNO)
	{
		File directory = new File(dir);
		File[] files = directory.listFiles();

		try
		{
			FileOutputStream output = new FileOutputStream(new File(dir + busNO
					+ ".zip"));
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
					output));

			for (int i = 0; i < files.length; i++)
			{
				InputStream in = new FileInputStream(files[i]);
				ZipEntry e = new ZipEntry(files[i].getName());
				out.putNextEntry(e);

				int len = 0;
				byte[] b = new byte[1024];
				while ((len = in.read(b)) != -1)
				{
					out.write(b, 0, len);
					out.flush();
				}

				out.closeEntry();
			}
			out.flush();
			output.flush();
			out.close();
			output.close();
			out = null;
			output = null;
			System.gc();
		}
		catch (FileNotFoundException e)
		{

			return false;
		}
		catch (IOException e)
		{

			return false;
		}

		return true;
	}

运行测试, 搞定!!

File的delete方法:

  /**
     * Deletes the file or directory denoted by this abstract pathname.  If
     * this pathname denotes a directory, then the directory must be empty in
     * order to be deleted.
     *
     * @return  <code>true</code> if and only if the file or directory is
     *          successfully deleted; <code>false</code> otherwise
     *
     * @throws  SecurityException
     *          If a security manager exists and its <code>{@link
     *          java.lang.SecurityManager#checkDelete}</code> method denies
     *          delete access to the file
     */
    public boolean delete() {
	SecurityManager security = System.getSecurityManager();
	if (security != null) {
	    security.checkDelete(path);
	}
	return fs.delete(this);


可见如果是文件, 可以删除, 

但如果是目录, 必须是空的才可以删除。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值