|
|
@@ -159,9 +159,48 @@ public class FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 移动文件(夹)
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * 源文件(夹)
|
|
|
+ * @param dest
|
|
|
+ * 目的文件(夹)
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
public static void move(File src, File dest) throws IOException {
|
|
|
- copy(src, dest);
|
|
|
- delete(src);
|
|
|
+ if (src.equals(dest)) {
|
|
|
+ throw new IOException("不可移动到相同路径下");
|
|
|
+ }
|
|
|
+ if (!src.isFile() && !dest.isFile() && isSub(src, dest)) {
|
|
|
+ throw new IOException("不可移动到子路径下");
|
|
|
+ }
|
|
|
+ if (!dest.exists()) {
|
|
|
+ if (dest.isDirectory()) {
|
|
|
+ dest.mkdirs();
|
|
|
+ } else {
|
|
|
+ dest.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (src.isFile()) {
|
|
|
+ if (dest.isDirectory()) {
|
|
|
+ dest = new File(dest, src.getName());
|
|
|
+ if (src.equals(dest)) {
|
|
|
+ throw new IOException("移动的源路径与目的路径不可相同");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ copy(src, dest);
|
|
|
+ delete(src);
|
|
|
+ } else {
|
|
|
+ if (dest.isFile()) {
|
|
|
+ throw new IOException("不可将文件夹移动到文件");
|
|
|
+ }
|
|
|
+ File[] files = src.listFiles();
|
|
|
+ for (File file : files) {
|
|
|
+ move(file, new File(dest, file.getName()));
|
|
|
+ }
|
|
|
+ src.delete();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static void copy(File src, File dest) throws IOException {
|
|
|
@@ -179,19 +218,6 @@ public class FileUtils {
|
|
|
out.close();
|
|
|
}
|
|
|
|
|
|
- public static boolean delete(File f) {
|
|
|
- boolean deleted = true;
|
|
|
- if ((f != null) && (f.exists())) {
|
|
|
- logger.trace("delete() Deleting file: {}", f);
|
|
|
- System.gc();
|
|
|
- System.runFinalization();
|
|
|
- deleted = f.delete();
|
|
|
- } else {
|
|
|
- logger.trace("delete() File doesn't exist: {}", f);
|
|
|
- }
|
|
|
- return deleted;
|
|
|
- }
|
|
|
-
|
|
|
private static void copyChannel(ReadableByteChannel source, WritableByteChannel dest) throws IOException {
|
|
|
ByteBuffer buffer = ByteBuffer.allocateDirect(32768);
|
|
|
|
|
|
@@ -209,6 +235,48 @@ public class FileUtils {
|
|
|
dest.write(buffer);
|
|
|
}
|
|
|
|
|
|
+ public static boolean delete(File f) {
|
|
|
+ boolean deleted = true;
|
|
|
+ if ((f != null) && (f.exists())) {
|
|
|
+ logger.trace("delete() Deleting file: {}", f);
|
|
|
+ System.gc();
|
|
|
+ System.runFinalization();
|
|
|
+ deleted = f.delete();
|
|
|
+ } else {
|
|
|
+ logger.trace("delete() File doesn't exist: {}", f);
|
|
|
+ }
|
|
|
+ return deleted;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查后面的文件夹是否是前面的文件夹的路径下
|
|
|
+ *
|
|
|
+ * @param dir
|
|
|
+ * @param dir2
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static boolean isSub(File dir, File dir2) throws IOException {
|
|
|
+ if (dir.equals(dir2)) {
|
|
|
+ throw new IOException("路径相同");
|
|
|
+ }
|
|
|
+ if (dir.isFile() || dir2.isFile()) {
|
|
|
+ throw new IOException("只对文件夹进行检测");
|
|
|
+ }
|
|
|
+ File parentFile = dir2.getParentFile();
|
|
|
+ if (parentFile == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (parentFile.equals(dir)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ if (!dir2.equals(parentFile)) {
|
|
|
+ return isSub(dir, parentFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 检查文件
|
|
|
*
|