Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can file.renameTo replace a existing file?

Tags:

java

file

file-io

// File (or directory) to be moved
File file = new File("filename");

// Destination directory
File dir = new File("directoryname");

// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
    // File was not successfully moved
    //can it be because file with file name already exists in destination?
}

If the file with name 'filename' already exists in the destination will it be replaced with a new one?

like image 484
akshay Avatar asked Jul 04 '11 08:07

akshay


1 Answers

According to Javadoc:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

like image 71
mnicky Avatar answered Oct 12 '22 14:10

mnicky