TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- import java.io.*;
- public class MoveFile{
- static String tempSpliter = System.getProperty("file.separator");
- public static void main(String agrs[]) throws Exception{
- fileMove("c:\h", "c:\w1");
- }
- /* 移动指定文件夹内的全部文件
- * @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 + tempSpliter + files[i].getName());
- // 成功,删除原文件
- files[i].delete();
- }
- File moveFile = new File(moveDir.getPath() + tempSpliter + files[i].getName());
- // 目标文件夹下存在的话,删除
- if (moveFile.exists()) {
- moveFile.delete();
- }
- files[i].renameTo(moveFile);
- }
- } catch (Exception e) {
- throw e;
- }
- }
- }
-
复制代码
源码下载:http://file.javaxxz.com/2014/11/6/235907203.zip |
|