使用java将所有文件从文件夹移动到其他文件夹 [英] Move all files from folder to other folder with java

查看:431
本文介绍了使用java将所有文件从文件夹移动到其他文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

用Java将文件从一个目录复制到另一个目录

如何使用java将所有文件从一个文件夹移动到其他文件夹?
我正在使用此代码:

How can I move all files from one folder to other folder with java? I'm using this code:

import java.io.File;

    public class Vlad {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            // File (or directory) to be moved
            File file = new File("C:\\Users\\i074924\\Desktop\\Test\\vlad.txt");

            // Destination directory
            File dir = new File("C:\\Users\\i074924\\Desktop\\Test2");

            // Move file to new directory
            boolean success = file.renameTo(new File(dir, file.getName()));
            if (!success) {
                System.out.print("not good");
            }
        }
    }

但它仅适用于一个特定的文件。

but it is working only for one specific file.

谢谢!!!

推荐答案

如果文件对象指向一个文件夹,你可以迭代它的内容

If a File object points to a folder you can iterate over it's content

File dir1 = new File("C:\\Users\\i074924\\Desktop\\Test");
if(dir1.isDirectory()) {
    File[] content = dir1.listFiles();
    for(int i = 0; i < content.length; i++) {
        //move content[i]
    }
}

这篇关于使用java将所有文件从文件夹移动到其他文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆