移动指定文件夹内的全部文件
更新时间:2009年01月12日 21:11:15 作者:
移动指定文件夹内的全部文件
import java.io.File;
public class FileMove {
/**
* 移动指定文件夹内的全部文件
*
* @param fromDir
* 要移动的文件目录
* @param toDir
* 目标文件目录
* @throws Exception
*/
public static void fileMove(String from, String to) throws Exception {
try {
File dir = new File(from);
// 文件一览
File[] files = dir.listFiles();
if (files == null)
return;
// 目标
File moveDir = new File(to);
if (!moveDir.exists()) {
moveDir.mkdirs();
}
// 文件移动
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fileMove(files[i].getPath(), to + "\\" + files[i].getName());
// 成功,删除原文件
files[i].delete();
}
File moveFile = new File(moveDir.getPath() + "\\"
+ files[i].getName());
// 目标文件夹下存在的话,删除
if (moveFile.exists()) {
moveFile.delete();
}
files[i].renameTo(moveFile);
}
} catch (Exception e) {
throw e;
}
}
}
public class FileMove {
/**
* 移动指定文件夹内的全部文件
*
* @param fromDir
* 要移动的文件目录
* @param toDir
* 目标文件目录
* @throws Exception
*/
public static void fileMove(String from, String to) throws Exception {
try {
File dir = new File(from);
// 文件一览
File[] files = dir.listFiles();
if (files == null)
return;
// 目标
File moveDir = new File(to);
if (!moveDir.exists()) {
moveDir.mkdirs();
}
// 文件移动
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fileMove(files[i].getPath(), to + "\\" + files[i].getName());
// 成功,删除原文件
files[i].delete();
}
File moveFile = new File(moveDir.getPath() + "\\"
+ files[i].getName());
// 目标文件夹下存在的话,删除
if (moveFile.exists()) {
moveFile.delete();
}
files[i].renameTo(moveFile);
}
} catch (Exception e) {
throw e;
}
}
}
相关文章
Java中HashMap和Hashtable及HashSet的区别
以下是对Java中HashMap和Hashtable及HashSet的区别进行了详细的分析介绍,需要的朋友可以过来参考下2013-09-09
Spring的Bean生命周期之BeanDefinition详解
这篇文章主要介绍了Spring的Bean生命周期之BeanDefinition详解,在spring bean创建过程 依赖 BeanDefinition 中的信息处理bean的生产,BeanDefinition 是 Spring Framework 中定义 Bean 的配置元信息接口,需要的朋友可以参考下2023-12-12


最新评论