文件操作工具类

2023-04-12
import java.io.File;
 
import java.io.FileFilter;
 
import java.io.FileInputStream;
 
import java.io.FileOutputStream;
 
import java.io.IOException;
 


/**
 
 * 

Title: 文件操作帮助类


*

Description: 文件操作函数集


* @version 1.0 */ public class FileUtils { /** * 创建目录,可以包含下级子目录 * @param _file 待创建的目录 * @param _isDir 参数_file是否是目录,如果传入的参数是文件,则创建该文件的父目录 * @return成功返回true,否则返回false */ public static boolean mkdir(File _file, boolean _isDir) { File dir = _file; if(!_isDir){ dir = _file.getParentFile(); } return dir.mkdirs(); } /** * 产生一个新的空文件,连同目录结构 * @param _file 空文件对象 * @return 如果创建成功返回true,否则返回false * @throws IOException */ public static boolean touch(File _file) throws IOException { if (_file.exists()) { return false; } else { mkdir(_file, false); _file.createNewFile(); return true; } } /** * 删除文件或整个目录。 * @param _file 文件或目录 */ public static void delete(File _file) { if (_file.isFile()) { _file.delete(); } else { File[] files = _file.listFiles(); for (int i = 0; i < files.length; i++) { delete(files[i]); } _file.delete(); } } /** * 删除文件或根目录以下的子目录。 * @param _file 文件或目录 */ public static void deleteBelow(File _file) { if (_file.isFile()) { _file.delete(); } else { File[] files = _file.listFiles(); for (int i = 0; i < files.length; i++) { deleteBelow(files[i]); files[i].delete(); } } } /** * 移动文件操作。 * @param _file1 源文件 * @param _file2 目标文件 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void move(File _file1, File _file2, boolean _isReplace) throws IOException { rawCopy(_file1, _file2, true, _isReplace); } /** * 文件拷贝功能 * @param _originalFile 原始文件 * @param _dirPath 新文件目录路径 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void copy(File _originalFile,File _dirPath,boolean _isReplace) throws IOException { mkdir(_dirPath,true); String newFileName =formatDirPath(_dirPath.getAbsolutePath())+ SystemInfo.fileSeparetor+ _originalFile.getName(); rawCopy(_originalFile, new File(newFileName), false, _isReplace); } /** * 移动整个目录 * @param _sourceDir 源目录对象 * @param _targetDir 目标目录对象 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void xmove(File _sourceDir, File _targetDir,boolean _isReplace) throws IOException{ xcopy(_sourceDir,_targetDir,null,_isReplace); delete(_sourceDir); } /** * 移动整个目录,仅仅移动经过过滤器处理的文件 * @param _sourceDir 源目录对象 * @param _targetDir 目标目录对象 * @param _filter 过滤器 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void xmove(File _sourceDir, File _targetDir,FileFilter _filter, boolean _isReplace) throws IOException{ xcopy(_sourceDir,_targetDir,_filter,_isReplace); delete(_sourceDir); } /** * 目录拷贝 * @param _sourceDir 源目录对象 * @param _targetDir 目标目录对象 * @param _filter 过滤器 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void xcopy(File _sourceDir, File _targetDir,FileFilter _filter, boolean _isReplace) throws IOException{ if (!_sourceDir.exists()) { throw new IOException("目录/文件:[" + _sourceDir.getAbsolutePath() + "]不存在"); } xcopyInner(_sourceDir, _targetDir, _filter, _isReplace); } /** * 拷贝源目录_sourceDir所有文件到目标目录_targetDir * @param _sourceDir 源目录对象 * @param _targetDir 目标目录对象 * @param _isReplace 如果目标文件存在,true表示覆盖目标文件,否则不覆盖 * @throws IOException */ public static void xcopy(File _sourceDir, File _targetDir, boolean _isReplace) throws IOException { xcopy(_sourceDir, _targetDir, null, _isReplace); } /** * 格式化文件目录,使其采用同一的形式,但该方法并不考虑目录是否存在。如将[e:\sam]转化为[e:\sam],将[e:\]转化为[e:],,将[e:/1]转化为[e:\1]将[e:ra]转化为[e:\ra],将[\ra]转化为[\ra],将[\ra\]转化为[\ra] * @param _dirPath 待格式化文件目录 * @return 格式化后的目录路径 */ public static String formatDirPath(String _dirPath) { StringBuffer strBuf = new StringBuffer(); String fs = SystemInfo.fileSeparetor; String temp = _dirPath; //window平台包含了驱动器符路径 int driveSpIndex = temp.indexOf(":"); //是否具有驱动器分割符 boolean flag = false; if (SystemInfo.osName.toLowerCase().indexOf("window") > -1) { //将文件目录分隔符转换为标准的Window平台分割符 temp = _dirPath.replace('/', '\\'); if (driveSpIndex > 0) { //处理驱动器符 strBuf.append(temp.substring(0, driveSpIndex + 1)); temp = temp.substring(driveSpIndex + 1); flag = true; } } //对于Unix,linux平台或Window平台中不包含驱动符的目录 if (!temp.startsWith(fs)) { strBuf.append(fs); strBuf.append(temp); temp = fs + temp; } else { strBuf.append(temp); } //去掉最后的"/" if (temp.endsWith(fs) && (//Unix, Linux平台或Window平台不包含驱动器符 (temp.length() >= 1) || //window平台包含驱动器符 (flag && !(_dirPath.length() == driveSpIndex + 2)))) { String str = strBuf.toString(); return str.substring(0, str.length() - 1); } else { return strBuf.toString(); } } /** * 文件拷贝,低级操作 * @param _sourceFile 源文件 * @param _targetFile 目标文件 * @param _isDelete 是否删除原来的文件 * @param _isReplace 目标文件存在时,是否替换目标文件 * @throws IOException */ private static void rawCopy(File _sourceFile,File _targetFile,boolean _isDelete,boolean _isReplace) throws IOException { if (_targetFile.exists()) { if (!_isReplace) { throw new IOException("在进行不替换文件操作,由于目标文件[" + _targetFile.getAbsolutePath() + "已经存在,操作中止"); } } else { touch(_targetFile); } IOUtils.write(new FileInputStream(_sourceFile),new FileOutputStream(_targetFile)); if (_isDelete) { _sourceFile.delete(); } } //xcopy inner handler private static void xcopyInner(File _sourceDir,File _targetDir,FileFilter _filter,boolean _isReplace) throws IOException { if (_sourceDir.isFile()) { // copy file copy(_sourceDir, _targetDir, _isReplace); } else { // copy directory File[] fileList = null; if (_filter == null) { fileList = _sourceDir.listFiles(); } else { fileList = _sourceDir.listFiles(_filter); } int sourcePathLength = _sourceDir.getAbsolutePath().length(); String relativePath = null; for (int i = 0; i < fileList.length; i++) { File file = fileList[i]; if (file.isFile()) { xcopy(file, _targetDir, _filter, _isReplace); } else { relativePath = file.getAbsolutePath().substring(sourcePathLength); xcopy(file,new File(formatDirPath(_targetDir.getAbsolutePath())+ SystemInfo.fileSeparetor+ relativePath),_filter,_isReplace); } } } } }

本文仅代表作者观点,版权归原创者所有,如需转载请在文中注明来源及作者名字。

免责声明:本文系转载编辑文章,仅作分享之用。如分享内容、图片侵犯到您的版权或非授权发布,请及时与我们联系进行审核处理或删除,您可以发送材料至邮箱:service@tojoy.com