import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* <p>Title: 文件操作帮助类</p>
* <p>Description: 文件操作函数集</p>
* @version 1.0
*/
public class FileUtils {
/**
* 创建目录,可以包含下级子目录
* @param _file 待创建的目录
* @param _isDir 参数<b>_file</b>是否是目录,如果传入的参数是文件,则创建该文件的父目录
* @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);
}
/**
* 拷贝源目录<b>_sourceDir</b>所有文件到目标目录<b>_targetDir</b>
* @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);
}
}
}
}
}
文件操作工具类
原创mb6434c781b2176 ©著作权
文章标签 目标文件 java 目标目录 文章分类 JavaScript 前端开发
-
ES工具操作测试类
ES工具操作测试类
User System elasticsearch -
RocketMQ工具操作测试类
RocketMQ工具操作测试类
异步消息 发送消息 ide -
ftp文件操作工具类
package com.gblfy.modular.utils;import org.apache.commons.net.ftp.FTPClient;import org.apaommons.n...
VSFTPD 系列 远程文件传输 java 上传 ftp服务器 -
JWT操作工具类分享
分享个人操作的JWT的工具类,基于jjwt库,这是Java圈子中最流行的JWT操作
jwt json java 状态码 -
HBase操作工具类
正文 添加依赖 <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId>
hbase haizhi 表名 apache hadoop -
idea中插件redishelper使用
1 命令行不知道大家在日常操作redis时用什么可视化工具呢?以前总觉得没有什么太好的可视化工具,于是问了一个业内朋友。对方回:你还用可视化工具?直接命令行呀,redis提供了这么多命令,操作起来行云流水。用可视化工具觉得很low。命令行的鄙视用工具的,用高端工具的鄙视低端工具的,鄙视链一直存在。虽然用命令行自己也可以,但是总感觉效率上不如用工具,在视觉上不那么直观。尤其是看json的时候,在命令
idea redis 插件 idea redis 插件收费 idea redis插件 idea 连接redis插件 redis scan 效率太慢