Java-io-file-deleteonexit
提供:Dev Guides
Java.io.File.deleteOnExit()メソッド
説明
- java.io.File.deleteOnExit()*メソッドは、仮想マシンの終了時に抽象パス名で定義されたファイルまたはディレクトリを削除します。 ファイルまたはディレクトリは、登録時に逆の順序で削除されます。
宣言
以下は* java.io.File.deleteOnExit()*メソッドの宣言です-
public void deleteOnExit()
パラメーター
NA
戻り値
メソッドは値を返しません。
例外
*SecurityException* -SecurityManager.checkWrite(java.lang.String)メソッドがファイルへの削除アクセスを拒否する場合
例
次の例は、java.io.File.deleteOnExit()メソッドの使用法を示しています。
package com.finddevguides;
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();
}
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
File path: C:\Users\BABAI\AppData\Local\Temp\tmp1307307616656986083.txt
File path: C:\Users\BABAI\AppData\Local\Temp\tmp4586112702019401940.tmp