移动指定文件夹内的全部文件
时间:2021-04-26 11:03:06|栏目:JAVA代码|点击: 次
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;
}
}
}


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




