1 6 7 package org.jfox.ioc.util; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.File ; 12 import java.io.FileFilter ; 13 import java.io.FileInputStream ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.OutputStream ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collections ; 22 import java.util.List ; 23 24 27 28 public class Files { 29 30 45 public static boolean delete(File file) { 46 if(!file.exists()) { 47 return true; 48 } 49 if(file.isDirectory()) { 50 return deleteDirectory(file); 51 } 52 else { 53 return file.delete(); 54 } 55 } 56 57 63 public static boolean deleteDirectory(File dir) { 64 65 if(!dir.exists()) return true; 66 boolean result = cleanDirectory(dir); 67 return result & dir.delete(); 68 } 69 70 75 public static boolean cleanDirectory(File directory) { 76 if(!directory.exists()) { 77 return true; 78 } 79 if(!directory.isDirectory()) { 80 return false; 81 } 82 boolean result = true; 83 84 File [] files = directory.listFiles(); 85 for(int i = 0; i < files.length; i++) { 86 File file = files[i]; 87 result = result && delete(file); 88 } 89 return result; 90 } 91 92 99 public static void deleteOnExit(File file) throws IOException { 100 if(file.isDirectory()) { 101 deleteDirectoryOnExit(file); 102 } 103 else { 104 file.deleteOnExit(); 105 } 106 } 107 108 114 private static void deleteDirectoryOnExit(File directory) 115 throws IOException { 116 if(!directory.exists()) { 117 return; 118 } 119 120 cleanDirectoryOnExit(directory); 121 directory.deleteOnExit(); 122 } 123 124 130 private static void cleanDirectoryOnExit(File directory) 131 throws IOException { 132 if(!directory.exists()) { 133 return; 135 } 136 137 if(!directory.isDirectory()) { 138 String message = directory + " is not a directory"; 139 throw new IllegalArgumentException (message); 140 } 141 142 IOException exception = null; 143 144 File [] files = directory.listFiles(); 145 for(int i = 0; i < files.length; i++) { 146 File file = files[i]; 147 try { 148 deleteOnExit(file); 149 } 150 catch(IOException ioe) { 151 exception = ioe; 152 } 153 } 154 155 if(null != exception) { 156 throw exception; 157 } 158 } 159 160 167 public static File copy(final File source, final File dir) throws IOException { 168 if(source.getParentFile().equals(dir)) { 169 return source; 170 } 171 if(!dir.exists()) { 172 dir.mkdirs(); 173 } 174 if(!dir.isDirectory()) { 175 throw new IOException ("Destination must be a directory."); 176 } 177 178 File target = new File (dir, source.getName()); 179 BufferedInputStream in = new BufferedInputStream (new FileInputStream (source)); 180 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (target)); 181 try { 182 IOs.write(in, out); 183 } 184 finally { 185 out.flush(); 186 IOs.close(in); 187 IOs.close(out); 188 } 189 return target; 190 } 191 192 198 public static File move(final File source, final File dir) throws IOException { 199 File target = copy(source, dir); 200 source.delete(); 201 return target; 202 } 203 204 212 public static void touch(File file) throws IOException { 213 OutputStream out = new java.io.FileOutputStream (file); 214 IOs.close(out); 215 } 216 217 224 public static URL [] toURLs(File [] files) throws IOException { 225 URL [] urls = new URL [files.length]; 226 227 for(int i = 0; i < urls.length; i++) { 228 urls[i] = files[i].toURL(); 229 } 230 231 return urls; 232 } 233 234 241 public static File toFile(URL url) { 242 if(url.getProtocol().equals("file") == false) { 243 return null; 244 } 245 else { 246 String filename = 247 url.getFile().replace('/', File.separatorChar); 248 return new File (filename); 249 } 250 } 251 252 255 public static String content(File file) throws IOException { 256 InputStream in = new java.io.FileInputStream (file); 257 try { 258 return IOs.toString(in); 259 } 260 finally { 261 IOs.close(in); 262 } 263 } 264 265 271 public static List listFiles(File dir, FileFilter filter) { 272 List files = new ArrayList (); 273 if(!dir.exists() || dir.isFile()) return files; 274 listFiles(files, dir, filter); 275 return files; 276 } 277 278 private static void listFiles(List filesList, File dir, FileFilter filter) { 279 File [] files = dir.listFiles(filter); 280 List temp = Arrays.asList(files); 281 Collections.sort(temp); 282 filesList.addAll(temp); 283 284 File [] subDirs = dir.listFiles(FileFilters.directoryFileFilter()); 285 for(int i = 0; i < subDirs.length; i++) { 286 listFiles(filesList, subDirs[i], filter); 287 } 288 } 289 } 290 291 | Popular Tags |