使用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]
}
}