Tuesday, June 9, 2009

To Move a File from One directory to another in Java

We can use File.renameTo() method to move a file from one folder to another.

// 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
}

No comments:

Post a Comment