renameTo method in java.io.file may not work always [For eg: During File Upload - if destination and source is in different file system, renameTo fails]
As per the documentation of java.io.file -renameTo
public boolean renameTo(File dest)
- Renames the file denoted by this abstract pathname.
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.
-
- Alternative approach is you can use org.apache.commons.io.FileUtils .
public static void copyFile(File srcFile, File destFile) throws IOException
- Copies a file to a new location preserving the file date.This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.