tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 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.

File Name  :  
com/bethecoder/tutorials/io/RenameFileTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
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


                    Print

   



Books Recommendation

 
 
bl  br