You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by oz...@apache.org on 2004/10/19 13:33:32 UTC
cvs commit: jakarta-slide/src/stores/org/apache/slide/store/util FileHelper.java
ozeigermann 2004/10/19 04:33:32
Modified: src/stores/org/apache/slide/store/util FileHelper.java
Log:
Augmented recursive move to allow moving of a single file
and added recursive copy in the same fashion
Revision Changes Path
1.5 +81 -26 jakarta-slide/src/stores/org/apache/slide/store/util/FileHelper.java
Index: FileHelper.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/util/FileHelper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FileHelper.java 28 Jul 2004 09:33:55 -0000 1.4
+++ FileHelper.java 19 Oct 2004 11:33:32 -0000 1.5
@@ -99,47 +99,102 @@
}
/**
- * Moves one directory to another. Existing files will be replaced.
+ * Moves one directory or file to another. Existing files will be replaced.
*
- * @param sourceDir directory to move from
- * @param targetDir directory to move to
+ * @param source file to move from
+ * @param target file to move to
* @throws IOException if an I/O error occurs (may result in partially done work)
*/
- public static void moveRec(File sourceDir, File targetDir) throws IOException {
+ public static void moveRec(File source, File target) throws IOException {
byte[] sharedBuffer = new byte[BUF_SIZE];
- moveRec(sourceDir, targetDir, sharedBuffer);
+ moveRec(source, target, sharedBuffer);
}
- static void moveRec(File sourceDir, File targetDir, byte[] sharedBuffer) throws IOException {
- if (sourceDir.isDirectory() && targetDir.isDirectory()) {
- File[] files = sourceDir.listFiles();
- for (int i = 0; i < files.length; i++) {
- File file = files[i];
- File targetFile = new File(targetDir, file.getName());
- if (file.isFile()) {
- if (targetFile.exists()) {
- targetFile.delete();
+ static void moveRec(File source, File target, byte[] sharedBuffer) throws IOException {
+ if (source.isDirectory()) {
+ if (!target.exists()) {
+ target.mkdirs();
+ }
+ if (target.isDirectory()) {
+
+ File[] files = source.listFiles();
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ File targetFile = new File(target, file.getName());
+ if (file.isFile()) {
+ if (targetFile.exists()) {
+ targetFile.delete();
+ }
+ if (!file.renameTo(targetFile)) {
+ copy(file, targetFile, sharedBuffer);
+ file.delete();
+ }
+ } else {
+ targetFile.mkdirs();
+ moveRec(file, targetFile);
}
- if (!file.renameTo(targetFile)) {
+ }
+ source.delete();
+ }
+ } else {
+ if (!target.isDirectory()) {
+ copy(source, target, sharedBuffer);
+ source.delete();
+ }
+ }
+ }
+
+ /**
+ * Copies one directory or file to another. Existing files will be replaced.
+ *
+ * @param source directory or file to copy from
+ * @param target directory or file to copy to
+ * @throws IOException if an I/O error occurs (may result in partially done work)
+ */
+ public static void copyRec(File source, File target) throws IOException {
+ byte[] sharedBuffer = new byte[BUF_SIZE];
+ copyRec(source, target, sharedBuffer);
+ }
+
+ static void copyRec(File source, File target, byte[] sharedBuffer) throws IOException {
+ if (source.isDirectory()) {
+ if (!target.exists()) {
+ target.mkdirs();
+ }
+ if (target.isDirectory()) {
+
+ File[] files = source.listFiles();
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ File targetFile = new File(target, file.getName());
+ if (file.isFile()) {
+ if (targetFile.exists()) {
+ targetFile.delete();
+ }
copy(file, targetFile, sharedBuffer);
- file.delete();
+ } else {
+ targetFile.mkdirs();
+ copyRec(file, targetFile);
}
- } else {
- targetFile.mkdirs();
- moveRec(file, targetFile);
}
}
- sourceDir.delete();
+ } else {
+ if (!target.isDirectory()) {
+ copy(source, target, sharedBuffer);
+ }
}
}
/**
* Copies one file to another using {@link #copy(InputStream, OutputStream)}.
*
- * @param input source file
- * @param output destination file
+ * @param input
+ * source file
+ * @param output
+ * destination file
* @return the number of bytes copied
- * @throws IOException if an I/O error occurs (may result in partially done work)
+ * @throws IOException
+ * if an I/O error occurs (may result in partially done work)
* @see #copy(InputStream, OutputStream)
*/
public static long copy(File input, File output) throws IOException {
---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org