Java临时文件何时被删除? [英] When are Java temporary files deleted?

查看:3057
本文介绍了Java临时文件何时被删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用方法在Java中创建一个临时文件

  File tmp = File.createTempFile(prefix,suffix); 

如果我不明确地调用 delete()方法,文件何时被删除?



直观上,它可能是JVM终止时,或早于垃圾收集器)或稍后(通过某些操作系统清除过程)。

不会自动从 JavaDoc


只是临时文件设施的一部分。为了
安排一个由这个方法创建的文件被自动删除,
使用deleteOnExit()方法。



<所以你必须明确地调用 deleteOnExit()


请求删除此抽象路径名
表示的文件或目录虚拟机终止。



Suppose I create a temporary file in Java with the method

File tmp = File.createTempFile(prefix, suffix);

If I do not explicity call the delete() method, when will the file be deleted?

As an intuition, it might be when the JVM terminates, or earlier (by the Garbage Collector), or later (by some Operating System sweeping process).

解决方案

The file won't be deleted automatically, from the JavaDoc:

This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.

So you have to explicitly call deleteOnExit():

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

这篇关于Java临时文件何时被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆