I want to move parent folder subfolders to another parent subfolder using java,below is the folder structure.
Source Dir : C:\Users\xxxxx\git\project\Parent
Desctinaton Dir: C:\Users\xxxxx\git\project\Parent\oldParent
Parent
oldParent
Subdolder1
Subfolder2
Now i want to move Subfolder1,2 into oldParent folder.I'm using below code to move but it is not working
import org.apache.commons.io.FileUtils;
public static void move(File sourceDir,File destinationDir){
try {
// FileUtils.copyDirectory(sourceDir, destinationDir);
FileUtils.moveDirectory(sourceDir, destinationDir);
} catch (IOException e) {
e.printStackTrace();
}
}
org.apache.commons.io.FileExistsException: Destination C:\Users\xxxxx\git\project\Parent\oldParent already exists
I've modified little bit code,just added and after moving files,not deleting the files,then i've used below code,Now it is moving the folders and deleting the folders after move.
import org.apache.commons.io.FileUtils;
public static void moveDirectory(File sourceDir, File destinationDir) throws IOException {
FileUtils.copyDirectory(sourceDir, destinationDir);
try {
File directory = new File(sourceDir);
File[] subdirs = directory.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);
for (File dir : subdirs) {
// System.out.println("Directory: " + dir.getName());
String name = dir.getName();
if (!name.equalsIgnoreCase(folderName)) {
FileUtils.forceDelete(new File(folderPath + name));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.