java中deleteOnExit()方法的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19012557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use of deleteOnExit() method in java
提问by Vishal Chugh
what is the difference between delete()and deleteOnExit()methods in java.io.Fileclass in Java?
Java中java.io.File类中的delete()和deleteOnExit()方法有什么区别?
采纳答案by Barny
delete() will delete the file at once, while deleteOnExit() will not delete the file, when you call it. Instead the file is deleted, when the program ends or more precise the virtual machine terminates.
delete() 将立即删除文件,而 deleteOnExit() 不会删除文件,当您调用它时。相反,文件被删除,当程序结束或更准确地说虚拟机终止时。
In case the virtual machines terminates not regulary, deleteOnExit() has no effect.
如果虚拟机不定期终止,deleteOnExit() 无效。
回答by MZaragoza
Delete() returns boolean Deletes the file or directory denoted by this abstract pathname.
Delete() 返回 boolean 删除此抽象路径名表示的文件或目录。
deleteOnExit() returns void Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
deleteOnExit() 返回 void 请求在虚拟机终止时删除此抽象路径名表示的文件或目录。
回答by MugunthanSelvaraj
File.delete() method deletes the file or throws an exception if the deletion fails. For example, if the file does not exist a NoSuchFileException is thrown. To delete a directory, the directory must be empty. This method returns true if the file is successfully deleted, else false (may be due to read/write permissions). This method is used when you want to delete a known file/directly.
File.delete() 方法删除文件或在删除失败时抛出异常。例如,如果文件不存在,则抛出 NoSuchFileException。要删除目录,目录必须为空。如果文件被成功删除,此方法返回 true,否则返回 false(可能是由于读/写权限)。 当您要删除/直接删除已知文件时使用此方法。
File.deleteOnExit() This method deletes the file or directory defined by the abstract path name when the virtual machine terminates. Files or directories are deleted in the reverse order as they are registered. The method does not return any value. This is useful when generating temporary files during program execution.
File.deleteOnExit() 该方法在虚拟机终止时删除抽象路径名定义的文件或目录。文件或目录以注册时的相反顺序删除。该方法不返回任何值。 这在程序执行期间生成临时文件时很有用。
回答by ticot55
File.deleteOnExit() documentation is miss leading. I'm using it on a project and in practice it works by deleting the file when the garbage collector runs (if there is no object referencing the file).
File.deleteOnExit() 文档丢失领先。我在一个项目中使用它,实际上它是通过在垃圾收集器运行时删除文件来工作的(如果没有对象引用该文件)。