package com.service; import java.io.File; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import com.utils.ConstConfig; public class FileUtil { private transient final static Logger log = LogManager.getLogger(FileUtil.class); /** * 删除文件夹 param folderPath 文件夹完整绝对路径 */ public static void delFolder(String folderPath) { try { delAllFile(folderPath, true); // 删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); // 删除空文件夹 } catch (Exception e) { log.error("删除文件夹出错!" + e.getMessage()); } } /** * 删除指定文件夹下所有文件 param path 文件夹完整绝对路径 param folderFlag 是否删除里面的文件夹 */ public static boolean delAllFile(String path, boolean folderFlag) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (folderFlag && temp.isDirectory()) { delAllFile(path + "/" + tempList[i], true);// 先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);// 再删除空文件夹 flag = true; } } flag = true; return flag; } /** * 删除指定文件夹下所有文件 param path 文件夹完整绝对路径 param folderFlag 是否删除里面的文件夹 */ public static boolean delAllFileByFolderName(String path, String folderName, boolean folderFlag) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (folderFlag && temp.isDirectory() && temp.getName().endsWith(folderName)) { delAllFile(path + "/" + tempList[i], true);// 先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);// 再删除空文件夹 flag = true; } else { delAllFileByFolderName(temp.getPath(), folderName, folderFlag); } } flag = true; return flag; } public static void main(String[] args) { String event = "delete folder path:"+ConstConfig.DELETE_PATH+"目录下的所有名称为"+ConstConfig.FOLDER_NAME+"文件夹"; try { delAllFileByFolderName(ConstConfig.DELETE_PATH, ConstConfig.FOLDER_NAME, true); } catch (Exception e) { log.error(event+" 失败!"); } log.info(event+" 成功!"); } /** * 删除指定时间前的文件 * <p> * Title: delFile * </p> * <p> * Description: * </p> * * @param path * @param time * @return */ public static boolean delFile(String path, Long time) { boolean flag = false; File file = new File(path); if (!file.exists()) { file.mkdirs(); return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { if (time != null && temp.lastModified() < time) { temp.delete(); } } } flag = true; return flag; } /** * 在指定路径下寻找文件 * * @param path * @param fileName * @return 文件路径 */ public static String findFile(String path, String fileName) { File[] files = new File(path).listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].getName().equals(fileName)) { // System.out.println(files[i].getAbsolutePath()); return files[i].getAbsolutePath(); } else { if (files[i].isDirectory()) { String destFileName = findFile(files[i].getAbsolutePath(), fileName); if (destFileName != null) { return destFileName; } } } } return null; } public static String standardFilePath(String filePath) { if (filePath == null || filePath.trim().equals("")) return filePath; filePath = filePath.trim(); if (File.separator.equals("\\")) { filePath = replaceAll(filePath, "/", "\\"); filePath = replaceAll(filePath, "\\\\", "\\"); } else if (File.separator.equals("/")) { filePath = replaceAll(filePath, "\\", "/"); filePath = replaceAll(filePath, "//", "/"); } return filePath; } public static String replaceAll(String str, String src, String dest) { if (str == null || src == null || dest == null || str.equals("") || src.equals("")) return str; int lensrc = src.length(); for (int idx = str.indexOf(src); idx >= 0; idx = str.indexOf(src)) str = (new StringBuilder()).append(str.substring(0, idx)).append(dest).append(str.substring(idx + lensrc)) .toString(); return str; } }


le10000 LV2
2022年7月4日
王文丽
2022年3月5日
暂无贡献等级
myou525
2021年3月29日
暂无贡献等级
未知错误 LV5
2020年8月11日
fengshaoye LV10
2020年5月20日
zym190605 LV1
2020年5月6日
freedom2017 LV14
2020年3月30日
2317696509 LV6
2019年10月31日
你好1314 LV1
2019年9月25日
Qweqweq LV2
2019年9月23日