|
Java > IO > Rename and Move File |
|
Rename and Move File
The following example shows how to rename a file and move a file from one directory to another directory.
|
package com.bethecoder.tutorials.io;
import java.io.File;
public class RenameFileTest {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("C:\\NEW\\NEW_SUB\\file_ops.txt");
File renameFile = new File("C:\\NEW\\NEW_SUB\\file_ops9.txt");
/**
* Rename file
* Returns TRUE if the file renamed successfully otherwise FALSE
*/
System.out.println("Renamed successfully : " + file.renameTo(renameFile));
/**
* Move file
*/
File file2 = new File("C:\\NEW\\NEW_SUB\\abc.txt");
File moveFile = new File("C:\\NEW\\AAA\\abc.txt");
System.out.println("Moved successfully : " + file2.renameTo(moveFile));
/**
* Rename folder
*/
File dir = new File("C:\\NEW\\CCCC");
File dirRenameTo = new File("C:\\NEW\\CCCC222");
System.out.println("Renamed successfully : " + dir.renameTo(dirRenameTo));
/**
* Move folder
*/
File dir2 = new File("C:\\NEW\\DDD");
File movDir = new File("C:\\NEW\\NEW_SUB\\DDD");
System.out.println("Moved successfully : " + dir2.renameTo(movDir));
}
}
|
| |
It gives the following output,
Renamed successfully : true
Moved successfully : true
Renamed successfully : true
Moved successfully : true
|
|