From 72a14719acbb52520634de5d75b89beeb9333ea3 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 11 Mar 2013 15:35:05 -0400 Subject: [PATCH] Now using apache commons for file operations. Hopefully we will get more success with deleting and copying stuff. May fix Inventories test failures. --- pom.xml | 28 +++++++ .../MultiverseCore/utils/FileUtils.java | 77 ++++--------------- 2 files changed, 43 insertions(+), 62 deletions(-) diff --git a/pom.xml b/pom.xml index ff280e6c..3b6158de 100644 --- a/pom.xml +++ b/pom.xml @@ -220,12 +220,40 @@ + + apache-commons-io + package + + shade + + + + + org.apache.commons:commons-io + + + + + org.apache.commons + org.apache.commons.multiverse + + + true + + + + org.apache.commons + commons-io + 1.3.2 + jar + compile + org.bukkit diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index 6452185a..4bc4a18a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -7,13 +7,10 @@ package com.onarandombox.MultiverseCore.utils; +import com.dumptruckman.minecraft.util.Logging; + import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.logging.Logger; /** @@ -31,16 +28,11 @@ public class FileUtils { * @return true if the folder was successfully deleted. */ public static boolean deleteFolder(File file) { - if (file.exists()) { - boolean ret = true; - // If the file exists, and it has more than one file in it. - if (file.isDirectory()) { - for (File f : file.listFiles()) { - ret = ret && deleteFolder(f); - } - } - return ret && file.delete(); - } else { + try { + org.apache.commons.io.FileUtils.deleteDirectory(file); + return true; + } catch (IOException e) { + Logging.warning(e.getMessage()); return false; } } @@ -52,16 +44,11 @@ public class FileUtils { * @return true if the contents were successfully deleted */ public static boolean deleteFolderContents(File file) { - if (file.exists()) { - boolean ret = true; - // If the file exists, and it has more than one file in it. - if (file.isDirectory()) { - for (File f : file.listFiles()) { - ret = ret && deleteFolder(f); - } - } - return ret; - } else { + try { + org.apache.commons.io.FileUtils.cleanDirectory(file); + return true; + } catch (IOException e) { + Logging.warning(e.getMessage()); return false; } } @@ -77,46 +64,12 @@ public class FileUtils { * @return if it had success */ public static boolean copyFolder(File source, File target, Logger log) { - InputStream in = null; - OutputStream out = null; try { - if (source.isDirectory()) { - - if (!target.exists()) - target.mkdir(); - - String[] children = source.list(); - // for (int i=0; i 0) { - out.write(buf, 0, len); - } - } + org.apache.commons.io.FileUtils.copyDirectory(source, target); return true; - } catch (FileNotFoundException e) { - log.warning("Exception while copying file: " + e.getMessage()); } catch (IOException e) { - log.warning("Exception while copying file: " + e.getMessage()); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException ignore) { } - } - if (out != null) { - try { - out.close(); - } catch (IOException ignore) { } - } + log.warning(e.getMessage()); + return false; } - return false; } }