[Java学习] Java文件操作工具类
2015-12-16 17:54
671 查看
移动目录或者文件, 将一个文件夹下所有文件移动到另一个文件夹下,将指定文件移动到目的目录,刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除
import java.io.File;
/**
*
* @Title : FileUtils
* @File Name : FileUtils.java
* @Description : 文件操作工具类
* @Date : 2014年8月10日
* @author : 王鸿运
* @version : 1.0
* @Others :
* @History 1.<br/>
* Date : <br/>
* Author : <br/>
* Modification: <br/>
*/
public class FileUtils {
public stati
4000
c final String FILE_SEP = System.getProperty("file.separator");
http://www.kmnk01.com/hxpfk/2015/qb_1216/305.html
/**
*
* @Function : moveFile
* @Desc : 移动目录或者文件
* @Author : 王鸿运
* @param srcFilePath 源文件(目录)路径
* @param destFilePath 目的文件(目录)路径
* @return true:操作成功,false:操作失败
*/
public static boolean moveFile(String srcFilePath, String destFilePath) {
File srcFile = new File(srcFilePath);
if (!srcFile.exists()) {
return false;
}
if (srcFile.isDirectory()) {
File[] subFiles = srcFile.listFiles();
for (File file : subFiles) {
moveFile(file.getAbsolutePath(), destFilePath +FILE_SEP+ file.getName());
}
}
File destFile = new File(destFilePath);http://www.kmnk01.com/hxpfk/2015/qb_1216/306.html
File destParentFile = destFile.getParentFile();
if (!destParentFile.exists()) {
destParentFile.mkdirs();
}
srcFile.renameTo(destFile);
return true;
}
http://www.kmnk01.com/hxpfk/2015/qb_1216/307.html
/**
* @Function : moveChildren
* @Desc : 指定目录下所有的子目录和文件到目的目录
* @Author : 王鸿运
* @param srcFilePath 源目录路径
* @param destFilePath 目的路径
* @return true:操作成功,false:操作失败
*/
public static boolean moveAllChildren(String srcFilePath, String destFilePath) {
File srcDir = new File(srcFilePath);
if (!srcDir.exists() || !srcDir.isDirectory()) {
return false;
}
File destDir = new File(destFilePath);
if (destDir.exists()) {
if ( destDir.isFile()) {
destDir.delete();
destDir = new File(destFilePath);
destDir.mkdir();
}
} else {
destDir.mkdirs();
}
http://www.kmnk01.com/hxpfk/2015/qb_1216/309.html
File[] children = srcDir.listFiles();
for (File file : children) {
moveFile(file.getAbsolutePath(), destFilePath);
}
http://www.kmnk03.com/hxpfk/bdf/323.html
return true;
}
/**
*
* @Function : moveFiles
* @Desc : 将指定文件移动到目的目录
* @Author : 王鸿运
* @param srcFiles 源文件,支持多个路径参数
* @param destDirPath 目的目录
* @return true:操作成功,false:操作失败
*/
public static boolean moveFiles(String destDirPath, String ... srcFiles){
if (srcFiles == null || srcFiles.length == 0) {
return false;
}
File destDir = new File(destDirPath);
http://www.kmnk01.com/hxpfk/2015/qb_1216/311.html
boolean destExists = true;
if (!destDir.exists()) {
destExists = destDir.mkdirs();
} else if(destDir.isFile()){
return false;
}
if (!destExists) {
return false;
}
for (String srcFilePath : srcFiles) {
File srcFile = new File(srcFilePath);
if (srcFile.exists() && srcFile.isFile()) {
srcFile.renameTo(new File(destDirPath + FILE_SEP + srcFile.getName()));
}
}
return true;
}
/**
*
* @Function : deleteSelfAndAllChildren
* @Desc : 刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除
* @Author : 王鸿运
* @param dirPath 源目录或文件路径
* @return true:删除成功,false:删除失败
*/http://www.kmnk03.com/hxpfk/npx/318.html
public static boolean deleteSelfAndAllChildren(String dirPath) {
File srcFile = new File(dirPath);
if (!srcFile.exists()) {
return false;
}
if (srcFile.isFile()) {
srcFile.delete();
} else {
File[] children = srcFile.listFiles();
http://www.kmnk03.com/hxpfk/bdf/320.html
if (children.length == 0) {
srcFile.delete();
} else {
for (File file : children) {//先删除子文件和目录
deleteSelfAndAllChildren(file.getAbsolutePath());
}
//再删除本身
srcFile.delete();
}
}
return true;
}
/**
*
* @Function : deleteAllChildren
* @Desc : 删除一个目录下所有的子目录和文件
* @Author : 王鸿运
* @param dirPath
* @return true:删除成功,false:删除失败
*/http://www.kmnk03.com/hxpfk/bdf/321.html
public static boolean deleteAllChildren(String dirPath){
File dir = new File(dirPath);
if (!dir.exists()) {
return false;http://www.kmnk03.com/hxpfk/bdf/322.html
}
File[] children = dir.listFiles();
for (File file : children) {
deleteSelfAndAllChildren(file.getAbsolutePath());
}
return true;
}
}kmnk03.com kmnk01.com
}www.kmnk03.com www.kmnk01.com
import java.io.File;
/**
*
* @Title : FileUtils
* @File Name : FileUtils.java
* @Description : 文件操作工具类
* @Date : 2014年8月10日
* @author : 王鸿运
* @version : 1.0
* @Others :
* @History 1.<br/>
* Date : <br/>
* Author : <br/>
* Modification: <br/>
*/
public class FileUtils {
public stati
4000
c final String FILE_SEP = System.getProperty("file.separator");
http://www.kmnk01.com/hxpfk/2015/qb_1216/305.html
/**
*
* @Function : moveFile
* @Desc : 移动目录或者文件
* @Author : 王鸿运
* @param srcFilePath 源文件(目录)路径
* @param destFilePath 目的文件(目录)路径
* @return true:操作成功,false:操作失败
*/
public static boolean moveFile(String srcFilePath, String destFilePath) {
File srcFile = new File(srcFilePath);
if (!srcFile.exists()) {
return false;
}
if (srcFile.isDirectory()) {
File[] subFiles = srcFile.listFiles();
for (File file : subFiles) {
moveFile(file.getAbsolutePath(), destFilePath +FILE_SEP+ file.getName());
}
}
File destFile = new File(destFilePath);http://www.kmnk01.com/hxpfk/2015/qb_1216/306.html
File destParentFile = destFile.getParentFile();
if (!destParentFile.exists()) {
destParentFile.mkdirs();
}
srcFile.renameTo(destFile);
return true;
}
http://www.kmnk01.com/hxpfk/2015/qb_1216/307.html
/**
* @Function : moveChildren
* @Desc : 指定目录下所有的子目录和文件到目的目录
* @Author : 王鸿运
* @param srcFilePath 源目录路径
* @param destFilePath 目的路径
* @return true:操作成功,false:操作失败
*/
public static boolean moveAllChildren(String srcFilePath, String destFilePath) {
File srcDir = new File(srcFilePath);
if (!srcDir.exists() || !srcDir.isDirectory()) {
return false;
}
File destDir = new File(destFilePath);
if (destDir.exists()) {
if ( destDir.isFile()) {
destDir.delete();
destDir = new File(destFilePath);
destDir.mkdir();
}
} else {
destDir.mkdirs();
}
http://www.kmnk01.com/hxpfk/2015/qb_1216/309.html
File[] children = srcDir.listFiles();
for (File file : children) {
moveFile(file.getAbsolutePath(), destFilePath);
}
http://www.kmnk03.com/hxpfk/bdf/323.html
return true;
}
/**
*
* @Function : moveFiles
* @Desc : 将指定文件移动到目的目录
* @Author : 王鸿运
* @param srcFiles 源文件,支持多个路径参数
* @param destDirPath 目的目录
* @return true:操作成功,false:操作失败
*/
public static boolean moveFiles(String destDirPath, String ... srcFiles){
if (srcFiles == null || srcFiles.length == 0) {
return false;
}
File destDir = new File(destDirPath);
http://www.kmnk01.com/hxpfk/2015/qb_1216/311.html
boolean destExists = true;
if (!destDir.exists()) {
destExists = destDir.mkdirs();
} else if(destDir.isFile()){
return false;
}
if (!destExists) {
return false;
}
for (String srcFilePath : srcFiles) {
File srcFile = new File(srcFilePath);
if (srcFile.exists() && srcFile.isFile()) {
srcFile.renameTo(new File(destDirPath + FILE_SEP + srcFile.getName()));
}
}
return true;
}
/**
*
* @Function : deleteSelfAndAllChildren
* @Desc : 刪除指定目录和其下所有子目录和文件,如果指定的是文件则直接删除
* @Author : 王鸿运
* @param dirPath 源目录或文件路径
* @return true:删除成功,false:删除失败
*/http://www.kmnk03.com/hxpfk/npx/318.html
public static boolean deleteSelfAndAllChildren(String dirPath) {
File srcFile = new File(dirPath);
if (!srcFile.exists()) {
return false;
}
if (srcFile.isFile()) {
srcFile.delete();
} else {
File[] children = srcFile.listFiles();
http://www.kmnk03.com/hxpfk/bdf/320.html
if (children.length == 0) {
srcFile.delete();
} else {
for (File file : children) {//先删除子文件和目录
deleteSelfAndAllChildren(file.getAbsolutePath());
}
//再删除本身
srcFile.delete();
}
}
return true;
}
/**
*
* @Function : deleteAllChildren
* @Desc : 删除一个目录下所有的子目录和文件
* @Author : 王鸿运
* @param dirPath
* @return true:删除成功,false:删除失败
*/http://www.kmnk03.com/hxpfk/bdf/321.html
public static boolean deleteAllChildren(String dirPath){
File dir = new File(dirPath);
if (!dir.exists()) {
return false;http://www.kmnk03.com/hxpfk/bdf/322.html
}
File[] children = dir.listFiles();
for (File file : children) {
deleteSelfAndAllChildren(file.getAbsolutePath());
}
return true;
}
}kmnk03.com kmnk01.com
}www.kmnk03.com www.kmnk01.com
相关文章推荐
- java集合——映射表+专用集合映射表类
- BigDecimal Java中最好用的保留小数点的方法
- java web导出文件
- JAVA中使用Formatter类进行输出格式的控制
- java 单例设计模式
- Myeclipse *.link用法
- Java基本数据类型
- Java安全管理器——SecurityManager
- MyEclipse 下用link 方式安装插件
- 在struts2中使用freemarker添加jsp标签支持
- spring配置双数据源
- Struts2 标签库讲解
- ibatis(学习2)-- spring + ibatis的事务管理
- java.lang.Class.getDeclaredMethod()方法
- java中的值传递和引用传递到底有什么区别
- spring保存一条数据库记录
- Java泛型详解
- Java与数据库数据类型对应表
- 用eclipse进行系统开发资料整理
- springMVC两种方式实现多文件上传及效率比较