Mercurial > dive4elements > framework
changeset 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 | 4d3298295a64 |
children | 81ae7948bff0 |
files | ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java |
diffstat | 2 files changed, 137 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Mon Jan 09 08:15:35 2012 +0000 +++ b/ChangeLog Mon Jan 09 10:44:43 2012 +0000 @@ -1,3 +1,9 @@ +2012-01-09 Ingo Weinzierl <ingo@intevation.de> + + * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java: + Added new functions to copy files (copyFile()) and to copy directories + (copyDirectory()). + 2012-01-09 Ingo Weinzierl <ingo@intevation.de> * artifact-database/src/main/java/de/intevation/artifactdatabase/state/State.java,
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Mon Jan 09 08:15:35 2012 +0000 +++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Mon Jan 09 10:44:43 2012 +0000 @@ -12,7 +12,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.OutputStream; +import java.nio.channels.FileChannel; import java.util.Stack; import java.util.List; @@ -399,5 +401,134 @@ } out.closeEntry(); } + + + /** + * Copies a <i>src</i> file to <i>target</i>. + * + * @param src A file (not a directory) that should be copied. + * @param target The destination. This might be a file or a directory. + * + * @return true, if <i>src</i> has been successfully copied; otherwise + * false. + */ + public static boolean copyFile(File src, File target) + throws IOException + { + if (src == null || !src.exists()) { + log.warn("Source file does not exist!"); + return false; + } + + if (!src.canRead()) { + log.warn("Cannot read Source file!"); + return false; + } + + if (src.isDirectory()) { + log.warn("Source is a directory!"); + return false; + } + + if (target.isDirectory()) { + target = new File(target, src.getName()); + } + + FileInputStream in = null; + FileOutputStream out = null; + + try { + in = new FileInputStream(src); + out = new FileOutputStream(target); + + FileChannel inChannel = in.getChannel(); + FileChannel outChannel = out.getChannel(); + + inChannel.transferTo(0l, inChannel.size(), outChannel); + + return true; + } + catch (IOException ioe) { + log.warn(ioe, ioe); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ioe) { /* do nothing here */ } + } + + if (out != null) { + try { + out.close(); + } + catch (IOException ioe) { /* do nothing here */ } + } + } + + return false; + } + + + /** + * Copies a directory <i>source</i> to a destination path <i>dest</i>. + * + * @param source A directory that should be copied. + * @param dest A destination directory which is created if it is not + * existing yet. + * + * @return true, if the directory has been successfully copied; otherwise + * false. + */ + public static boolean copyDirectory(final File source, final File dest) { + if (source == null || !source.exists()) { + log.warn("Source directory does not exist!"); + return false; + } + + if (!source.isDirectory()) { + log.warn("Source is not a directory!"); + return false; + } + + if (dest == null) { + log.warn("Destination directory is null!"); + return false; + } + + if (!dest.exists()) { + if (!dest.mkdir()) { + log.warn("Cannot create destination directory!"); + return false; + } + } + + File[] children = source.listFiles(); + int failed = 0; + + if (children != null && children.length > 0) { + for (File child: children) { + if (child.isFile()) { + try { + if (!copyFile(child, dest)) { + failed++; + } + } + catch (IOException ioe) { + log.warn(ioe, ioe); + failed++; + } + } + else if (child.isDirectory()) { + copyDirectory(child, new File(dest, child.getName())); + } + } + } + + log.debug("Failed to copy " + failed + " files."); + + return true; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :