The usual file tools, not much to say, go directly to the code:
/** * FileUtil.java * Copyright ® 2011 Dou Haining * All right reserved */ package org.aiyu.core.common.util.file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import org.aiyu.core.common.util.CollectionUtil; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.joda.time.DateTime; /** * <p>File tool class * * <p>General file tool class, which can be used to perform some common operations on files. * * @author Dou Haining, [email protected] * @since AiyuCommonCore-1.0 * @version AiyuCommonCore-1.0 */ public abstract class FileUtil { /** file type*/ public enum FileType { FILE , //file DIRECTORY; //Folder } //log object private static final Logger logger = Logger.getLogger(FileUtil.class); /** * <p>Create temporary file pathname * * @param fileName filename * * @return export file path name * * @modify Dou Haining, 2014-05-04 */ public static String createTempFileName(String fileName) { String tempFileName = null; if (StringUtils.isNotBlank(fileName)) { File rootPathFile = new File("..\\temp\\"); try { tempFileName = FilenameUtils.concat(rootPathFile.getCanonicalPath() , FileUtil.getDatePath(DateTime.now())); FileUtils.forceMkdir(new File(tempFileName)); tempFileName = FilenameUtils.concat(tempFileName , FileUtil.getDateFileName(fileName , DateTime.now())); } catch (IOException ex) { ex.printStackTrace(); } } return tempFileName; } /** * <p>Get the date path corresponding to the specified time object * * @param dateTime time object * * @return The date path corresponding to the specified time object, if the incoming date is October 25, 2012, the output result is 2012\10\25 * * @modify Dou Haining, 2014-05-07 */ public static String getDatePath(DateTime dateTime) { String dateTypeValue = null; if (dateTime != null) { dateTypeValue = FilenameUtils.concat(dateTime.toString("yyyy") , dateTime.toString("MM")); dateTypeValue = FilenameUtils.concat(dateTypeValue , dateTime.toString("dd")); } return dateTypeValue; } /** * <p>Get the file name corresponding to the current date and time object (this method only inserts a string representing the current date and time at the end of the file name) * * @param fileName file name * @param dateTime file creation time to avoid file name duplication * * @return The file name corresponding to the current date and time object. For example, if the file name is D:\\Readme.doc and the incoming date is 12:35 on October 25, 2012, the output result is Readme_20121025_1235.doc * * @modify Dou Haining, 2014-05-07 */ public static String getDateFileName(String fileName , DateTime dateTime) { String dateFileName = null; if (fileName != null) { String baseName = FilenameUtils.getBaseName(fileName); String extName = FilenameUtils.getExtension(fileName); dateFileName = baseName + "_" + dateTime.toString("yyyyMMdd_hhmm") + "." + extName; } return dateFileName; } /** * <p>Delete the specified path file * * @param filePath file path * * @modify Dou Haining, 2014-06-10 */ public static void deleteFile(String filePath) { if (StringUtils.isNotBlank(filePath)) { File file = new File(filePath); if (file.exists()) { try { FileUtils.forceDelete(file); } catch (Exception ex) { ex.printStackTrace(); } } else { FileUtil.logger.debug("FileUtil -> deleteFile : The folder corresponding to filePath does not exist"); } } else { FileUtil.logger.debug("FileUtil -> deleteFile : filePath is null!"); } } /** * <p>Load parameter object from specified path file * * @param filePath file path * * @return the loaded parameter object * * @modify Dou Haining, 2015-05-05 */ public static Object loadObject(String filePath) throws Exception { Object value = null; if (StringUtils.isNotBlank(filePath)) { // read file from file system FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; try { fileInputStream = new FileInputStream(new File(filePath)); objectInputStream = new ObjectInputStream(fileInputStream); value = objectInputStream.readObject(); } catch (Exception ex) { ex.printStackTrace(); } finally { IOUtils.closeQuietly(objectInputStream); IOUtils.closeQuietly(fileInputStream); } } return value; } /** * <p>Save the parameter object to the specified path file * * @param object parameter object * @param directoryPath file path * @param filename filename * @param appendDate whether to append the date to the end of the filename * * @modify Dou Haining, 2012-10-30 */ public static void saveObject(Object object , String directoryPath , String filename , boolean appendDate) throws Exception { if (object != null && StringUtils.isNotBlank(directoryPath) && StringUtils.isNotBlank(filename)) { // save the object to the file system FileUtils.forceMkdir(new File(directoryPath)); String filePath = null; if (appendDate) { filePath = FilenameUtils.concat(directoryPath , FileUtil.getDateFileName(filename , DateTime.now())); } filePath = FilenameUtils.concat(directoryPath , filename); FileOutputStream fileOutputStream = null; ObjectOutputStream objectOutputStream = null; try { fileOutputStream = new FileOutputStream(new File(filePath)); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(object); } catch (Exception ex) { ex.printStackTrace(); } finally { IOUtils.closeQuietly(objectOutputStream); IOUtils.closeQuietly(fileOutputStream); } } } /** * <p>Get a list of all files * * @param directoryFile The file root path object where the file list to be obtained is located * * @return a list of all files (including subfolders) under the specified file root path * * @modify Dou Haining, 2016-01-05 */ public static List getFileList(File directoryFile) { return FileUtil.getFileList(directoryFile , false); } /** * <p>Get a list of all files * * @param directoryFile The file root path object where the file list to be obtained is located * @param debug whether to output debugging information * * @return a list of all files (including subfolders) under the specified file root path * * @modify Dou Haining, 2016-01-05 */ public static List getFileList(File directoryFile , boolean debug) { return FileUtil.getFileList(directoryFile , FileType.FILE , false , debug); } /** * <p>Get a list of all folders * * @param directoryFile The file root path object where the folder list to be obtained is located * @param isLastSubDirectory only returns leaf node folders * * @return a list of all folders (including subfolders) under the specified file root path * * @modify Dou Haining, 2016-01-05 */ public static List getDirectoryList(File directoryFile , boolean isLastSubDirectory) { return FileUtil.getFileList(directoryFile , FileType.DIRECTORY , isLastSubDirectory , false); } /** * <p>Get a list of all folders * * @param directoryFile The file root path object where the folder list to be obtained is located * @param isLastSubDirectory only returns leaf node folders * @param debug whether to output debugging information * * @return a list of all folders (including subfolders) under the specified file root path * * @modify Dou Haining, 2016-01-05 */ public static List getDirectoryList(File directoryFile , boolean isLastSubDirectory , boolean debug) { return FileUtil.getFileList(directoryFile , FileType.DIRECTORY , isLastSubDirectory , debug); } /** * <p>Get a list of all files * * @param directoryFile The file root path object where the file list to be obtained is located * @param fileType file type * @param isLastSubDirectory only returns leaf node folders * @param debug whether to output debugging information * * @return a list of all files (including subfolders) under the specified file root path * * @modify Dou Haining, 2014-02-03 */ protected static List getFileList(File directoryFile , FileType fileType , boolean isLastSubDirectory , boolean debug) { List fileList = new ArrayList(); if (directoryFile != null && directoryFile.exists()) { File[] fileArray = directoryFile.listFiles(); for (int i = 0 ; i < fileArray.length ; i++) { switch (fileType) { case FILE : if (fileArray[i].isDirectory()) { // process the folder fileList.addAll(FileUtil.getFileList(fileArray[i] , fileType , isLastSubDirectory , debug)); } else { // process the file if (debug) { try { FileUtil.logger.debug("FileUtil -> getFileList : add file : " + fileArray[i].getCanonicalPath()); } catch (IOException ex) { ex.printStackTrace(); } } fileList.add(fileArray[i]); } break; case DIRECTORY : if (fileArray[i].isDirectory()) { // process the folder List subDirectoryList = FileUtil.getFileList(fileArray[i] , fileType , isLastSubDirectory , debug); if (isLastSubDirectory) { if (CollectionUtil.isEmpty(subDirectoryList)) { if (debug) { try { FileUtil.logger.debug("FileUtil -> getFileList : add directory : " + fileArray[i].getCanonicalPath()); } catch (IOException ex) { ex.printStackTrace(); } } fileList.add(fileArray[i]); } } else { if (debug) { try { FileUtil.logger.debug("FileUtil -> getFileList : add directory : " + fileArray[i].getCanonicalPath()); } catch (IOException ex) { ex.printStackTrace(); } } fileList.add(fileArray[i]); fileList.addAll(subDirectoryList); } } break; } } } else { FileUtil.logger.debug("FileUtil -> getFileList : directoryFile is null!"); } return fileList; } }