Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java @ 364:0723859f776f
Added new FileTools functions to copy files and directories.
artifacts/trunk@3627 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 09 Jan 2012 10:44:43 +0000 |
parents | 666417d5781c |
children | c489256a188b |
comparison
equal
deleted
inserted
replaced
363:4d3298295a64 | 364:0723859f776f |
---|---|
10 import java.io.BufferedInputStream; | 10 import java.io.BufferedInputStream; |
11 import java.io.File; | 11 import java.io.File; |
12 import java.io.IOException; | 12 import java.io.IOException; |
13 import java.io.InputStream; | 13 import java.io.InputStream; |
14 import java.io.FileInputStream; | 14 import java.io.FileInputStream; |
15 import java.io.FileOutputStream; | |
15 import java.io.OutputStream; | 16 import java.io.OutputStream; |
17 import java.nio.channels.FileChannel; | |
16 | 18 |
17 import java.util.Stack; | 19 import java.util.Stack; |
18 import java.util.List; | 20 import java.util.List; |
19 import java.util.Set; | 21 import java.util.Set; |
20 import java.util.HashSet; | 22 import java.util.HashSet; |
397 catch (IOException ioe) {} | 399 catch (IOException ioe) {} |
398 } | 400 } |
399 } | 401 } |
400 out.closeEntry(); | 402 out.closeEntry(); |
401 } | 403 } |
404 | |
405 | |
406 /** | |
407 * Copies a <i>src</i> file to <i>target</i>. | |
408 * | |
409 * @param src A file (not a directory) that should be copied. | |
410 * @param target The destination. This might be a file or a directory. | |
411 * | |
412 * @return true, if <i>src</i> has been successfully copied; otherwise | |
413 * false. | |
414 */ | |
415 public static boolean copyFile(File src, File target) | |
416 throws IOException | |
417 { | |
418 if (src == null || !src.exists()) { | |
419 log.warn("Source file does not exist!"); | |
420 return false; | |
421 } | |
422 | |
423 if (!src.canRead()) { | |
424 log.warn("Cannot read Source file!"); | |
425 return false; | |
426 } | |
427 | |
428 if (src.isDirectory()) { | |
429 log.warn("Source is a directory!"); | |
430 return false; | |
431 } | |
432 | |
433 if (target.isDirectory()) { | |
434 target = new File(target, src.getName()); | |
435 } | |
436 | |
437 FileInputStream in = null; | |
438 FileOutputStream out = null; | |
439 | |
440 try { | |
441 in = new FileInputStream(src); | |
442 out = new FileOutputStream(target); | |
443 | |
444 FileChannel inChannel = in.getChannel(); | |
445 FileChannel outChannel = out.getChannel(); | |
446 | |
447 inChannel.transferTo(0l, inChannel.size(), outChannel); | |
448 | |
449 return true; | |
450 } | |
451 catch (IOException ioe) { | |
452 log.warn(ioe, ioe); | |
453 } | |
454 finally { | |
455 if (in != null) { | |
456 try { | |
457 in.close(); | |
458 } | |
459 catch (IOException ioe) { /* do nothing here */ } | |
460 } | |
461 | |
462 if (out != null) { | |
463 try { | |
464 out.close(); | |
465 } | |
466 catch (IOException ioe) { /* do nothing here */ } | |
467 } | |
468 } | |
469 | |
470 return false; | |
471 } | |
472 | |
473 | |
474 /** | |
475 * Copies a directory <i>source</i> to a destination path <i>dest</i>. | |
476 * | |
477 * @param source A directory that should be copied. | |
478 * @param dest A destination directory which is created if it is not | |
479 * existing yet. | |
480 * | |
481 * @return true, if the directory has been successfully copied; otherwise | |
482 * false. | |
483 */ | |
484 public static boolean copyDirectory(final File source, final File dest) { | |
485 if (source == null || !source.exists()) { | |
486 log.warn("Source directory does not exist!"); | |
487 return false; | |
488 } | |
489 | |
490 if (!source.isDirectory()) { | |
491 log.warn("Source is not a directory!"); | |
492 return false; | |
493 } | |
494 | |
495 if (dest == null) { | |
496 log.warn("Destination directory is null!"); | |
497 return false; | |
498 } | |
499 | |
500 if (!dest.exists()) { | |
501 if (!dest.mkdir()) { | |
502 log.warn("Cannot create destination directory!"); | |
503 return false; | |
504 } | |
505 } | |
506 | |
507 File[] children = source.listFiles(); | |
508 int failed = 0; | |
509 | |
510 if (children != null && children.length > 0) { | |
511 for (File child: children) { | |
512 if (child.isFile()) { | |
513 try { | |
514 if (!copyFile(child, dest)) { | |
515 failed++; | |
516 } | |
517 } | |
518 catch (IOException ioe) { | |
519 log.warn(ioe, ioe); | |
520 failed++; | |
521 } | |
522 } | |
523 else if (child.isDirectory()) { | |
524 copyDirectory(child, new File(dest, child.getName())); | |
525 } | |
526 } | |
527 } | |
528 | |
529 log.debug("Failed to copy " + failed + " files."); | |
530 | |
531 return true; | |
532 } | |
402 } | 533 } |
403 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 534 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |