|
|
@@ -101,8 +101,8 @@ public class FileUtils {
|
|
|
* @param file
|
|
|
* 文件(夹)
|
|
|
*/
|
|
|
- public static void deleteDir(File file) {
|
|
|
- deleteDir(file, null);
|
|
|
+ public static void delete(File file) {
|
|
|
+ delete(file, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -113,8 +113,8 @@ public class FileUtils {
|
|
|
* @param printLogger
|
|
|
* 是否输出logger信息
|
|
|
*/
|
|
|
- public static void deleteDir(File file, boolean printLogger) {
|
|
|
- deleteDir(file, null, printLogger);
|
|
|
+ public static void delete(File file, boolean printLogger) {
|
|
|
+ delete(file, null, printLogger);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,8 +125,8 @@ public class FileUtils {
|
|
|
* @param filter
|
|
|
* 文件过滤
|
|
|
*/
|
|
|
- public static void deleteDir(File file, FileFilter filter) {
|
|
|
- deleteDir(file, filter, true);
|
|
|
+ public static void delete(File file, FileFilter filter) {
|
|
|
+ delete(file, filter, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -139,14 +139,14 @@ public class FileUtils {
|
|
|
* @param printLogger
|
|
|
* 是否输出logger信息
|
|
|
*/
|
|
|
- public static void deleteDir(File file, FileFilter filter, boolean printLogger) {
|
|
|
+ public static void delete(File file, FileFilter filter, boolean printLogger) {
|
|
|
if (file == null) {
|
|
|
return;
|
|
|
}
|
|
|
if (file.isDirectory()) {
|
|
|
File[] files = file.listFiles();
|
|
|
for (File f : files) {
|
|
|
- deleteDir(f, filter, printLogger);
|
|
|
+ delete(f, filter, printLogger);
|
|
|
}
|
|
|
}
|
|
|
// 如果文件不符合条件,则不删除
|
|
|
@@ -169,55 +169,51 @@ public class FileUtils {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static void move(File src, File dest) throws IOException {
|
|
|
+ copy(src, dest);
|
|
|
+ delete(src);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制文件(夹)
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * 源文件(夹)
|
|
|
+ * @param dest
|
|
|
+ * 目的文件(夹)
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void copy(File src, File dest) throws IOException {
|
|
|
if (src.equals(dest)) {
|
|
|
- throw new IOException("不可移动到相同路径下");
|
|
|
+ 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();
|
|
|
- }
|
|
|
+ throw new IOException("不可置于子路径下");
|
|
|
}
|
|
|
if (src.isFile()) {
|
|
|
- if (dest.isDirectory()) {
|
|
|
+ if (dest.exists() && dest.isDirectory()) {
|
|
|
dest = new File(dest, src.getName());
|
|
|
- if (src.equals(dest)) {
|
|
|
- throw new IOException("移动的源路径与目的路径不可相同");
|
|
|
- }
|
|
|
+ copy(src, dest);
|
|
|
+ return;
|
|
|
}
|
|
|
- copy(src, dest);
|
|
|
- delete(src);
|
|
|
+ ReadableByteChannel in = Channels.newChannel(new FileInputStream(src));
|
|
|
+ WritableByteChannel out = Channels.newChannel(new FileOutputStream(dest));
|
|
|
+ copyChannel(in, out);
|
|
|
+ in.close();
|
|
|
+ out.close();
|
|
|
} else {
|
|
|
- if (dest.isFile()) {
|
|
|
- throw new IOException("不可将文件夹移动到文件");
|
|
|
+ if (dest.exists() && dest.isFile()) {
|
|
|
+ throw new IOException("不可将文件夹置于文件下");
|
|
|
+ }
|
|
|
+ if (!dest.exists()) {
|
|
|
+ dest.mkdirs();
|
|
|
}
|
|
|
File[] files = src.listFiles();
|
|
|
for (File file : files) {
|
|
|
- move(file, new File(dest, file.getName()));
|
|
|
+ copy(file, new File(dest, file.getName()));
|
|
|
}
|
|
|
- src.delete();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void copy(File src, File dest) throws IOException {
|
|
|
- logger.trace("copy() - source file: {}, exists: {}", src.getAbsolutePath(), Boolean.valueOf(src.exists()));
|
|
|
- ReadableByteChannel in = Channels.newChannel(new FileInputStream(src));
|
|
|
-
|
|
|
- logger.trace("copy() - dest file: {}, exists: {}", dest.getAbsolutePath(), Boolean.valueOf(dest.exists()));
|
|
|
- WritableByteChannel out = Channels.newChannel(new FileOutputStream(dest));
|
|
|
-
|
|
|
- logger.trace("copy() Copying...");
|
|
|
- copyChannel(in, out);
|
|
|
-
|
|
|
- logger.trace("copy() Closing files...");
|
|
|
- in.close();
|
|
|
- out.close();
|
|
|
- }
|
|
|
-
|
|
|
private static void copyChannel(ReadableByteChannel source, WritableByteChannel dest) throws IOException {
|
|
|
ByteBuffer buffer = ByteBuffer.allocateDirect(32768);
|
|
|
|
|
|
@@ -235,19 +231,6 @@ 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;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 检查后面的文件夹是否是前面的文件夹的路径下
|
|
|
*
|