In this section we will be showing on how to move a file or directory from one folder to another in java. The sample source uses the renameTo method under the File class of java.io package.
package com.javatutorialhq.tutorial;
import java.io.File;
/**
* This java sample code shows how to
* move file or directory in java
* demonstrating use of renameTo method
* Property of javatutorialhq.com
* All Rights Reserved
* Version 1.0
* 06/30/2012
*/
public class MovieFileIO {
/**
* @param args
*/
public static void main(String[] args) {
File fSource = new File("C:\\temp\\source");
File fTarget = new File("C:\\temp\\target");
boolean testSuccess = fSource.renameTo(fTarget);
if(testSuccess){
System.out.println("Rename succeeded");
}
else
System.out.println("Rename failed");
}
}