1 7 package java.io; 8 9 import java.util.*; 10 import java.io.File ; 11 12 17 18 class DeleteOnExitHook { 19 private static DeleteOnExitHook instance = null; 20 21 private static LinkedHashSet<String > files = new LinkedHashSet<String >(); 22 23 static DeleteOnExitHook hook() { 24 if (instance == null) 25 instance = new DeleteOnExitHook (); 26 27 return instance; 28 } 29 30 private DeleteOnExitHook() {} 31 32 static void add(String file) { 33 synchronized(files) { 34 if(files == null) 35 throw new IllegalStateException ("Shutdown in progress"); 36 37 files.add(file); 38 } 39 } 40 41 void run() { 42 LinkedHashSet<String > theFiles; 43 44 synchronized (files) { 45 theFiles = files; 46 files = null; 47 } 48 49 ArrayList<String > toBeDeleted = new ArrayList<String >(theFiles); 50 51 Collections.reverse(toBeDeleted); 54 for (String filename : toBeDeleted) { 55 (new File (filename)).delete(); 56 } 57 } 58 } 59 60 | Popular Tags |