| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.FileNotFoundException; |
| 38 | | | import java.io.FileOutputStream; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.InputStream; |
| 41 | | | import java.io.OutputStream; |
| 42 | | | import java.io.Reader; |
| 43 | | | import java.io.Writer; |
| 44 | | | import java.util.ArrayList; |
| 45 | | | import java.util.List; |
| 46 | | (1) | import java.util.logging.Level; |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | (2) | |
| 53 | | | |
| 54 | | | @author |
| 55 | | | @author |
| 56 | | | |
| 57 | | | public final class FileUtils |
| 58 | | | { |
| 59 | | | private static final int RND_FILENAME_FACTOR = 100000; |
| 60 | | | |
| 61 | | | private static final int BUFFER_SIZE = 4096; |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | private FileUtils () |
| 67 | 0 | | { |
| 68 | | | |
| 69 | 0 | | } |
| 70 | | | |
| 71 | | | |
| 72 | | | <code></code> |
| 73 | | | <code></code> |
| 74 | | | |
| 75 | | | @param |
| 76 | | | @param |
| 77 | | | @throws |
| 78 | | | |
| 79 | | | public static void copy (File src, File destinationDir) |
| 80 | | | throws IOException |
| 81 | | | { |
| 82 | 0 | | if (src.isFile()) |
| 83 | | | { |
| 84 | 0 | | copyFile(src, destinationDir); |
| 85 | | | } |
| 86 | 0 | | else if (src.isDirectory()) |
| 87 | | | { |
| 88 | 0 | | final File subdir = new File(destinationDir, src.getName()); |
| 89 | 0 | | if (!subdir.exists()) |
| 90 | | | { |
| 91 | 0 | | if (!subdir.mkdir()) |
| 92 | | | { |
| 93 | 0 | | throw new IOException("Failed to create subdir '" + subdir |
| 94 | | | + "'."); |
| 95 | | | } |
| 96 | | | } |
| 97 | 0 | | final File [] files = src.listFiles(); |
| 98 | 0 | | for (int i = 0; i < files.length; i++) |
| 99 | | | { |
| 100 | 0 | | copy(files[i], subdir); |
| 101 | | | } |
| 102 | | | } |
| 103 | 0 | | } |
| 104 | | | |
| 105 | | | |
| 106 | | | <code></code> |
| 107 | | | <code></code> |
| 108 | | | |
| 109 | | | @param |
| 110 | | | @param |
| 111 | | | @throws |
| 112 | | | @throws |
| 113 | | | |
| 114 | | | public static void copyFile (File src, File dest) |
| 115 | | | throws FileNotFoundException, IOException |
| 116 | | | { |
| 117 | 0 | | FileInputStream in = null; |
| 118 | 0 | | FileOutputStream out = null; |
| 119 | | | try |
| 120 | | | { |
| 121 | 0 | | in = new FileInputStream(src); |
| 122 | 0 | | if (dest.isDirectory()) |
| 123 | | | { |
| 124 | 0 | | out = new FileOutputStream(new File(dest, src.getName())); |
| 125 | | | } |
| 126 | | | else |
| 127 | | | { |
| 128 | 0 | | out = new FileOutputStream(dest); |
| 129 | | | } |
| 130 | 0 | | copy(in, out); |
| 131 | | | } |
| 132 | | | finally |
| 133 | | | { |
| 134 | 0 | | close(in); |
| 135 | 0 | | close(out); |
| 136 | 0 | | } |
| 137 | 0 | | } |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | |
| 143 | | | @param |
| 144 | | | @param |
| 145 | | | @throws |
| 146 | | | |
| 147 | | | |
| 148 | | | public static void copy (InputStream in, OutputStream out) |
| 149 | | | throws IOException |
| 150 | | | { |
| 151 | 0 | | final byte[] buffer = new byte[BUFFER_SIZE]; |
| 152 | | | int read; |
| 153 | 0 | | while ((read = in.read(buffer)) != -1) |
| 154 | | | { |
| 155 | 0 | | out.write(buffer, 0, read); |
| 156 | | | } |
| 157 | 0 | | } |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | <code></code> |
| 162 | | | <code></code> |
| 163 | | | |
| 164 | | | |
| 165 | | | <pre> |
| 166 | | | |
| 167 | | | </pre> |
| 168 | | | |
| 169 | | | @param |
| 170 | | | @param |
| 171 | | | @throws |
| 172 | | | |
| 173 | | | public static void copySlashStar (File srcDir, File dst) |
| 174 | | | throws IOException |
| 175 | | | { |
| 176 | 0 | | if (srcDir.isDirectory()) |
| 177 | | | { |
| 178 | 0 | | final File[] files = srcDir.listFiles(); |
| 179 | 0 | | for (int i = 0; i < files.length; i++) |
| 180 | | | { |
| 181 | 0 | | copy(files[i], dst); |
| 182 | | | } |
| 183 | 0 | | } |
| 184 | | | else |
| 185 | | | { |
| 186 | 0 | | throw new IllegalArgumentException("Souce must be a directory. ('" |
| 187 | | | + srcDir + "')"); |
| 188 | | | } |
| 189 | 0 | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | @param |
| 194 | | | @param |
| 195 | | | @return |
| 196 | | | @throws |
| 197 | | | |
| 198 | | | public static File createTempDir (File baseDir, String prefix) |
| 199 | | | throws IOException |
| 200 | | | { |
| 201 | 0 | (3) | final String dirname = prefix |
| 202 | | | + String.valueOf((int) (Math.random() * RND_FILENAME_FACTOR)); |
| 203 | | | |
| 204 | 0 | | final File tempDir = new File(baseDir, dirname); |
| 205 | | | |
| 206 | 0 | | if (! tempDir.mkdir()) |
| 207 | | | { |
| 208 | 0 | | throw new IOException("Cannot create temp directory '" |
| 209 | | | + tempDir + "'"); |
| 210 | | | } |
| 211 | 0 | | return tempDir; |
| 212 | | | } |
| 213 | | | |
| 214 | | | |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | {@link } |
| 220 | | | <code></code> |
| 221 | | | |
| 222 | | | @param |
| 223 | | | @deprecated |
| 224 | | | |
| 225 | | | public static void close (InputStream in) |
| 226 | | | { |
| 227 | 0 | | IoUtil.close(in); |
| 228 | 0 | | } |
| 229 | | | |
| 230 | | | |
| 231 | | | |
| 232 | | | |
| 233 | | | |
| 234 | | | |
| 235 | | | {@link } |
| 236 | | | <code></code> |
| 237 | | | |
| 238 | | | @param |
| 239 | | | @deprecated |
| 240 | | | |
| 241 | | | public static void close (OutputStream out) |
| 242 | | | { |
| 243 | 0 | | IoUtil.close(out); |
| 244 | 0 | | } |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | |
| 249 | | | |
| 250 | | | {@link } |
| 251 | | | <code></code> |
| 252 | | | |
| 253 | | | @param |
| 254 | | | @deprecated |
| 255 | | | |
| 256 | | | public static void safeClose (Reader reader) |
| 257 | | | { |
| 258 | 0 | | IoUtil.close(reader); |
| 259 | 0 | | } |
| 260 | | | |
| 261 | | | |
| 262 | | | |
| 263 | | | |
| 264 | | | |
| 265 | | | {@link } |
| 266 | | | <code></code> |
| 267 | | | |
| 268 | | | @param |
| 269 | | | @deprecated |
| 270 | | | |
| 271 | | | public static void safeClose (Writer writer) |
| 272 | | | { |
| 273 | 0 | | IoUtil.close(writer); |
| 274 | 0 | | } |
| 275 | | | |
| 276 | | | |
| 277 | | | |
| 278 | | | |
| 279 | | | |
| 280 | | | |
| 281 | | | <pre> |
| 282 | | | |
| 283 | | | </pre> |
| 284 | | | @param |
| 285 | | | @param |
| 286 | | | @return<code></code> |
| 287 | | | <code></code> |
| 288 | | | |
| 289 | | | public static List findFile (File path, String pattern) |
| 290 | | | { |
| 291 | 0 | | final List ret = new ArrayList(); |
| 292 | | | |
| 293 | | | |
| 294 | 0 | | if (!path.exists()) |
| 295 | | | { |
| 296 | 0 | | throw new IllegalArgumentException( |
| 297 | | | "The specified path does not exist! ('" |
| 298 | | | + path + "')"); |
| 299 | | | } |
| 300 | | | |
| 301 | 0 | | findFile(path, pattern, ret); |
| 302 | | | |
| 303 | 0 | | return ret; |
| 304 | | | } |
| 305 | | | |
| 306 | | | private static void findFile (File file, String pattern, List found) |
| 307 | | | { |
| 308 | 0 | | if (file.isDirectory()) |
| 309 | | | { |
| 310 | 0 | | final File[] files = file.listFiles(); |
| 311 | 0 | | for (int i = 0; i < files.length; i++) |
| 312 | | | { |
| 313 | 0 | | findFile(files[i], pattern, found); |
| 314 | | | } |
| 315 | 0 | | } |
| 316 | | | else |
| 317 | | | { |
| 318 | 0 | | if (file.getName().matches(pattern)) |
| 319 | | | { |
| 320 | 0 | | found.add(file); |
| 321 | | | } |
| 322 | | | } |
| 323 | 0 | | } |
| 324 | | | |
| 325 | | | |
| 326 | | | |
| 327 | | | |
| 328 | | | |
| 329 | | | <pre> |
| 330 | | | |
| 331 | | | </pre> |
| 332 | | | @param |
| 333 | | | @throws |
| 334 | | | |
| 335 | | | public static void rmdir (File file) |
| 336 | | | throws IOException |
| 337 | | | { |
| 338 | 0 | | if (file == null) |
| 339 | | | { |
| 340 | | | |
| 341 | | | } |
| 342 | 0 | | else if (file.isDirectory()) |
| 343 | | | { |
| 344 | 0 | | final File [] files = file.listFiles(); |
| 345 | 0 | | for (int i = 0; i < files.length; i++) |
| 346 | | | { |
| 347 | 0 | | rmdir(files[i]); |
| 348 | | | } |
| 349 | 0 | | if (!file.delete()) |
| 350 | | | { |
| 351 | 0 | | throw new IOException("Failed to delete directory " + file + "."); |
| 352 | | | } |
| 353 | 0 | | } |
| 354 | | | else |
| 355 | | | { |
| 356 | 0 | | if (!file.delete()) |
| 357 | | | { |
| 358 | 0 | | throw new IOException("Failed to delete file " + file + "."); |
| 359 | | | } |
| 360 | | | } |
| 361 | 0 | | } |
| 362 | | | |
| 363 | | | |
| 364 | | | <code></code> |
| 365 | | | <code></code> |
| 366 | | | @param |
| 367 | | | @param |
| 368 | | | @return |
| 369 | | | @throws |
| 370 | | | |
| 371 | | | public static String getRelativePath (File baseDir, File file) |
| 372 | | | throws IOException |
| 373 | | | { |
| 374 | 0 | | final String base = baseDir.getCanonicalPath(); |
| 375 | 0 | | String fileName = file.getCanonicalPath(); |
| 376 | | | |
| 377 | 0 | | if (fileName.startsWith(base)) |
| 378 | | | { |
| 379 | 0 | | fileName = fileName.substring(base.length()); |
| 380 | 0 | | if (fileName.charAt(0) == '/') |
| 381 | | | { |
| 382 | 0 | | fileName = fileName.substring(1); |
| 383 | | | } |
| 384 | | | } |
| 385 | | | else |
| 386 | | | { |
| 387 | 0 | | throw new RuntimeException("Cannot add file '" + file |
| 388 | | | + "' with different baseDir '" + baseDir + "'."); |
| 389 | | | } |
| 390 | 0 | | return fileName; |
| 391 | | | } |
| 392 | | | |
| 393 | | | |
| 394 | | | <code></code> |
| 395 | | | |
| 396 | | | @param |
| 397 | | | @param |
| 398 | | | @throws |
| 399 | | | |
| 400 | | | public static void rename (File aFile, File dest) |
| 401 | | | throws IOException |
| 402 | | | { |
| 403 | 0 | | if (!aFile.renameTo(dest)) |
| 404 | | | { |
| 405 | 0 | | throw new IOException("Failed to rename " + aFile + " to " + dest); |
| 406 | | | } |
| 407 | 0 | | } |
| 408 | | | |
| 409 | | | |
| 410 | | | <code></code> |
| 411 | | | |
| 412 | | | @param |
| 413 | | | @throws |
| 414 | | | |
| 415 | | | public static void delete (File aFile) |
| 416 | | | throws IOException |
| 417 | | | { |
| 418 | 0 | | if (aFile.exists()) |
| 419 | | | { |
| 420 | 0 | | if (!aFile.delete()) |
| 421 | | | { |
| 422 | 0 | | throw new IOException("Failed to delete " + aFile); |
| 423 | | | } |
| 424 | | | } |
| 425 | 0 | | } |
| 426 | | | |
| 427 | | | |
| 428 | | | |
| 429 | | | |
| 430 | | | @param |
| 431 | | | @throws |
| 432 | | | @see |
| 433 | | | |
| 434 | | | public static void mkdirs (File dirs) |
| 435 | | | throws IOException |
| 436 | | | { |
| 437 | 0 | | if (!dirs.exists() || !dirs.isDirectory()) |
| 438 | | | { |
| 439 | 0 | | if (!dirs.mkdirs()) |
| 440 | | | { |
| 441 | 0 | | throw new IOException("Failed to create directories " + dirs); |
| 442 | | | } |
| 443 | | | } |
| 444 | 0 | | } |
| 445 | | | |
| 446 | | | |
| 447 | | | |
| 448 | | | |
| 449 | | | @param |
| 450 | | | @throws |
| 451 | | | @see |
| 452 | | | |
| 453 | | | public static void mkdir (File dir) |
| 454 | | | throws IOException |
| 455 | | | { |
| 456 | 0 | | if (!dir.exists() || !dir.isDirectory()) |
| 457 | | | { |
| 458 | 0 | | if (!dir.mkdir()) |
| 459 | | | { |
| 460 | 0 | | throw new IOException("Failed to create directory " + dir); |
| 461 | | | } |
| 462 | | | } |
| 463 | 0 | | } |
| 464 | | | |
| 465 | | | |
| 466 | | | |
| 467 | | | @param |
| 468 | | | @throws |
| 469 | | | @see |
| 470 | | | |
| 471 | | | public static void createNewFile (File newFile) |
| 472 | | | throws IOException |
| 473 | | | { |
| 474 | 0 | | if (!newFile.createNewFile()) |
| 475 | | | { |
| 476 | 0 | | throw new IOException("Failed to create new File " + newFile); |
| 477 | | | } |
| 478 | 0 | | } |
| 479 | | | } |