Bläddra i källkod

重新组织FileUtils中delete、copy、move等方法的结构

sunyj 8 år sedan
förälder
incheckning
f80d7d6e90

+ 2 - 2
src/main/java/com/uas/report/crystal2jasper/CrystalToJasper.java

@@ -195,7 +195,7 @@ public class CrystalToJasper {
 		String reportName = FileUtils.removeSuffix(rptZipFile.getName());
 		File reportDir = remainHierarchy ? new File(outDir, reportName) : outDir;
 		File uncompressedDir = new File(reportDir, "uncompressed");
-		FileUtils.deleteDir(uncompressedDir, false);
+		FileUtils.delete(uncompressedDir, false);
 		FileUtils.initDir(uncompressedDir);
 
 		// 先解压缩rpt压缩包
@@ -256,7 +256,7 @@ public class CrystalToJasper {
 		processSubReportPath(reportFile, subReportNames);
 
 		// 完成转换后删除解压缩的文件
-		FileUtils.deleteDir(uncompressedDir, false);
+		FileUtils.delete(uncompressedDir, false);
 	}
 
 	/**

+ 2 - 2
src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -325,7 +325,7 @@ public class FileServiceImpl implements FileService {
 		if (!file.exists()) {
 			throw new ReportException("文件不存在,不必删除:" + filePath);
 		}
-		FileUtils.deleteDir(file);
+		FileUtils.delete(file);
 		return filePath;
 	}
 
@@ -526,7 +526,7 @@ public class FileServiceImpl implements FileService {
 		Executable command = new Executable() {
 			@Override
 			public String execute() {
-				FileUtils.deleteDir(new File(ReportConstants.GENERATED_FILES_ABSOLUTE_PATH), new FileFilter() {
+				FileUtils.delete(new File(ReportConstants.GENERATED_FILES_ABSOLUTE_PATH), new FileFilter() {
 					@Override
 					public boolean accept(File file) {
 						// 只删除已过期的文件

+ 38 - 55
src/main/java/com/uas/report/util/FileUtils.java

@@ -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;
-	}
-
 	/**
 	 * 检查后面的文件夹是否是前面的文件夹的路径下
 	 *