1 2 24 25 26 27 28 29 package com.lutris.util; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 34 41 class TmpDir { 42 File dir; 43 44 51 public TmpDir (String path, 52 String baseName) 53 throws IOException { 54 int dirNum = 0; 55 56 59 while (true) { 60 dir = new File (path, baseName + "." + System.currentTimeMillis () + 61 "." + dirNum + ".tmp"); 62 if (dir.mkdirs ()) { 63 break; 64 } 65 if (!dir.exists ()) { 66 69 throw new IOException ("no permission to create directory " + 70 dir.getAbsolutePath ()); 71 } 72 dirNum++; 73 if (dirNum >= 25) { 74 throw new IOException ("failed to create tmp directory named in the " + 75 "form \"" + dir.getAbsolutePath () + 76 "\" after " + dirNum + " trys"); 77 78 } 79 } 80 } 81 82 87 public File file () { 88 return dir; 89 } 90 91 94 void recursiveDelete (File dirPath) { 95 String [] ls = dirPath.list (); 96 97 for (int idx = 0; idx < ls.length; idx++) { 98 File file = new File (dirPath, ls [idx]); 99 if (file.isDirectory ()) 100 recursiveDelete (file); 101 file.delete (); 102 } 103 } 104 105 106 111 public void delete () 112 throws IOException { 113 recursiveDelete (dir); 114 if (dir.exists ()) { 115 throw new IOException ("Unable to delete directory hierarchy \"" + 116 dir.getAbsolutePath () + "\""); 117 } 118 } 119 120 127 public File mkdirs (String path) 128 throws IOException { 129 File newDir = new File (dir.getAbsolutePath (), path); 130 newDir.mkdirs (); 131 if (!newDir.exists ()) { 132 throw new IOException ("Unable to create directory \"" + 133 dir.getAbsolutePath () + "\""); 134 } 135 return newDir; 136 } 137 } 138 | Popular Tags |