java.io.File.deleteOnExit()-生成临时文件,删除临时文件
2016-04-26 17:24
639 查看
转载:http://www.tutorialspoint.com/java/io/file_deleteonexit.htm
public void deleteOnExit()
Let us compile and run the above program, this will produce the following result:
File path: C:\Users\BABAI\AppData\Local\Temp\tmp1307307616656986083.txt
File path: C:\Users\BABAI\AppData\Local\Temp\tmp4586112702019401940.tmp
Description
The java.io.File.deleteOnExit() 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.Declaration
Following is the declaration for java.io.File.deleteOnExit() method:public void deleteOnExit()
Return Value
The method does not return any value.Exception
SecurityException -- If SecurityManager.checkWrite(java.lang.String) method denies delete access to the file
Example
The following example shows the usage of java.io.File.deleteOnExit() method.package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; try{ // creates temporary file f = File.createTempFile("tmp", ".txt"); // prints absolute path System.out.println("File path: "+f.getAbsolutePath()); // deletes file when the virtual machine terminate f.deleteOnExit(); // creates temporary file f = File.createTempFile("tmp", null); // prints absolute path System.out.print("File path: "+f.getAbsolutePath()); // deletes file when the virtual machine terminate f.deleteOnExit(); }catch(Exception e){ // if any error occurs e.printStackTrace(); } } }
Let us compile and run the above program, this will produce the following result:
File path: C:\Users\BABAI\AppData\Local\Temp\tmp1307307616656986083.txt
File path: C:\Users\BABAI\AppData\Local\Temp\tmp4586112702019401940.tmp
相关文章推荐
- java 时区问题 SimpleDateFormat 时区大全
- Java:使用synchronized和Lock对象获取对象锁
- Spring AOP 实现业务日志记录
- Strut2 Spring hibernate的优缺点
- [java] 多态实现的JVM调用过程
- RxJava学习(四)
- java的注入Deprecated
- Java Socket
- java基础数据遍历(4)删除数组中重复数字
- [Spring入门点滴]利用构造函数和setter注入
- Spring 表达式语言(Spring EL)
- Java方法传值和传址分析
- Java枚举类型小结
- form表单回写技术java实现
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
- Spring 基础知识
- Java线程之1.4版多生产多消费者示例(三)
- Java IO:CharArrayWriter使用及源码分析
- eclipse中maven报错--Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
- [Java] 接口(Interface)与 抽象类 (Abstract)使用规则和区别