package com.keyword.automation.base.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

/**
 * 文件工具类
 * 
 * @author airpy
 *
 */
public class FileUtils {
	private static final Logger log = Logger.getLogger(FileUtils.class);

	/**
	 * 创建目录(若父目录不存在则一并创建)
	 * 
	 * @param dir
	 *            目录
	 */
	public static void mkdir(String dir) {
		try {
			File dirPath = new File(dir);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
		} catch (Exception e) {
			log.error("创建目录[" + dir + "]操作失败: " + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 新建文件
	 * 
	 * @param fileName
	 *            包含绝对路径的文件名(E:\project\src\123.txt)
	 * @param content
	 *            文件内容
	 */
	public static void createNewFile(String fileName, String content) {
		try {
			File filePath = new File(fileName);
			if (!filePath.exists()) {
				filePath.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(filePath);
			PrintWriter printWriter = new PrintWriter(fileWriter);
			printWriter.println(content);
			printWriter.flush();
			printWriter.close();
			fileWriter.close();
		} catch (Exception e) {
			log.error("新建文件[" + fileName + "]操作失败: " + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param fileName
	 *            包含绝对路径的文件名(E:\project\src\123.txt)
	 */
	public static void deleteFile(String fileName) {
		try {
			File deleteFile = new File(fileName);
			if (!deleteFile.exists()) {
				deleteFile.delete();
			}
		} catch (Exception e) {
			log.error("删除文件[" + fileName + "]操作失败: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void deleteAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			log.error("传入的参数不存在[" + path + "]");
			return;
		}
		if (!file.isDirectory()) {
			log.error("传入的参数为目录[" + path + "]");
			return;
		}
		String[] childFiles = file.list();
		File temp = null;
		for (String tempChild : childFiles) {
			//File.separator与系统有关的默认名称分隔符
            //在UNIX系统上，此字段的值为'/'；在Microsoft Windows系统上，它为 '\'
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempChild);
			} else {
				temp = new File(path + File.separator + tempChild);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				deleteAllFile(path + "/" + childFiles); 
				deleteFolder(path + "/" + childFiles);
			}
		}
	}
	
	public static void deleteFolder(String folderPath) {
		try {
			deleteAllFile(folderPath);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

@@ -829,20 +829,16 @@
 ile file
-Path
  = new F
@@ -859,46 +859,37 @@
 %0A%09%09%09
-if (!filePath.exists()) %7B%0A%09
+deleteFile(fileName);%0A
 %09%09%09file
-Path
 .cre
@@ -902,21 +902,16 @@
 File();%0A
-%09%09%09%7D%0A
 %09%09%09FileW
@@ -948,20 +948,16 @@
 ter(file
-Path
 );%0A%09%09%09Pr
@@ -1394,23 +1394,17 @@
 %09%09%09File 
-deleteF
+f
 ile = ne
@@ -1429,23 +1429,17 @@
 %09%09%09if (!
-deleteF
+f
 ile.exis
@@ -1450,23 +1450,78 @@
 ) %7B%0A%09%09%09%09
-deleteF
+log.warn(%22%E6%82%A8%E8%BE%93%E5%85%A5%E7%9A%84%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%5B%22 + fileName + %22%5D%22);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09f
 ile.dele
@@ -1522,29 +1522,24 @@
 e.delete();%0A
-%09%09%09%7D%0A
 %09%09%7D catch (E

modify fileutils