Reputation: 51
I am trying to move directory forcefully that means if already exist then overwrite without asking.
Code :
import java.io.IOException;
import java.lang.System;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class MoveDirectory {
private static void movefilesandfolder(String sourceFilevar,String destinationFilevar)
{
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
movefilesandfolder("C:\\FTPDownload\\Downloaded\\epi141225_0001","C:\\FTPDownload\\In_Progress\\epi141225_0001");
}
}
Error output:
source=C:\FTPDownload\Downloaded\epi141225_0001
destination=C:\FTPDownload\In_Progress\epi141225_0001
java.nio.file.DirectoryNotEmptyException: C:\FTPDownload\In_Progress\epi141225_0001
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:286)
at java.nio.file.Files.move(Files.java:1345)
at MoveDirectory.movefilesandfolder(MoveDirectory.java:22)
at MoveDirectory.main(MoveDirectory.java:36)
This code works if Folder not present on destination path but fails if already exist. This code doesn't work even if empty folder is present on destination path.
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
In this call option i am using is not working for folder. This code Tested for file and it worked for already exist file as well.
But I want to move/overwrite folder.
Upvotes: 1
Views: 6773
Reputation: 1
You can try using Path.resolve(other Path)
method like this
Files.move(sourceFile, destinationFile.resolve(Paths.get(sourceFile).getFileName()), StandardCopyOption.REPLACE_EXISTING)
Java docs, give the best explanation about this.
Upvotes: 0
Reputation: 831
You can delete destination directory before moving, or, if you want to merge directories together, loop your directory files and move anyone in new folder
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// DELETE DIRECTORY
}
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
or
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// for each file in sourceFile
// Files.move file ...
}else{
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 1
Reputation: 31290
javadoc:
public static Path move(Path source,
Path target,
CopyOption... options)
throws IOException
Move or rename a file to a target file.
By default, this method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect. If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved. This method may be invoked to move an empty directory.
Upvotes: 0