importorg.apache.commons.io.FileUtils;import org.apache.commons.io.filefilter.*;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;import java.io.*;/*** 文件工具箱
*
*@authorleizhimin 2008-12-15 13:59:16*/
public final classFileToolkit {private static final Log log = LogFactory.getLog(FileToolkit.class);/*** 复制文件或者目录,复制前后文件完全一样。
*
*@paramresFilePath 源文件路径
*@paramdistFolder 目标文件夹
* @IOException 当操作发生异常时抛出*/
public static void copyFile(String resFilePath, String distFolder) throwsIOException {
File resFile= newFile(resFilePath);
File distFile= newFile(distFolder);if(resFile.isDirectory()) {
FileUtils.copyDirectoryToDirectory(resFile, distFile);
}else if(resFile.isFile()) {
FileUtils.copyFileToDirectory(resFile, distFile,true);
}
}/*** 删除一个文件或者目录
*
*@paramtargetPath 文件或者目录路径
* @IOException 当操作发生异常时抛出*/
public static void deleteFile(String targetPath) throwsIOException {
File targetFile= newFile(targetPath);if(targetFile.isDirectory()) {
FileUtils.deleteDirectory(targetFile);
}else if(targetFile.isFile()) {
targetFile.delete();
}
}/*** 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建。
*
*@paramresFilePath 源文件路径
*@paramdistFolder 目标文件夹
* @IOException 当操作发生异常时抛出*/
public static void moveFile(String resFilePath, String distFolder) throwsIOException {
File resFile= newFile(resFilePath);
File distFile= newFile(distFolder);if(resFile.isDirectory()) {
FileUtils.moveDirectoryToDirectory(resFile, distFile,true);
}else if(resFile.isFile()) {
FileUtils.moveFileToDirectory(resFile, distFile,true);
}
}/*** 重命名文件或文件夹
*
*@paramresFilePath 源文件路径
*@paramnewFileName 重命名
*@return操作成功标识*/
public static booleanrenameFile(String resFilePath, String newFileName) {
String newFilePath= StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" +newFileName);
File resFile= newFile(resFilePath);
File newFile= newFile(newFilePath);returnresFile.renameTo(newFile);
}/*** 读取文件或者目录的大小
*
*@paramdistFilePath 目标文件或者文件夹
*@return文件或者目录的大小,如果获取失败,则返回-1*/
public static longgenFileSize(String distFilePath) {
File distFile= newFile(distFilePath);if(distFile.isFile()) {returndistFile.length();
}else if(distFile.isDirectory()) {returnFileUtils.sizeOfDirectory(distFile);
}return -1L;
}/*** 判断一个文件是否存在
*
*@paramfilePath 文件路径
*@return存在返回true,否则返回false*/
public static booleanisExist(String filePath) {return newFile(filePath).exists();
}/*** 本地某个目录下的文件列表(不递归)
*
*@paramfolder ftp上的某个目录
*@paramsuffix 文件的后缀名(比如.mov.xml)
*@return文件名称列表*/
public staticString[] listFilebySuffix(String folder, String suffix) {
IOFileFilter fileFilter1= newSuffixFileFilter(suffix);
IOFileFilter fileFilter2= newNotFileFilter(DirectoryFileFilter.INSTANCE);
FilenameFilter filenameFilter= newAndFileFilter(fileFilter1, fileFilter2);return newFile(folder).list(filenameFilter);
}/*** 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
*
*@paramres 原字符串
*@paramfilePath 文件路径
*@return成功标记*/
public static booleanstring2File(String res, String filePath) {boolean flag = true;
BufferedReader bufferedReader= null;
BufferedWriter bufferedWriter= null;try{
File distFile= newFile(filePath);if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
bufferedReader= new BufferedReader(newStringReader(res));
bufferedWriter= new BufferedWriter(newFileWriter(distFile));char buf[] = new char[1024]; //字符缓冲区
intlen;while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf,0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
}catch(IOException e) {
flag= false;
e.printStackTrace();
}returnflag;
}
}