Java File.renameTo(File dest)

Syntax

File.renameTo(File dest) has the following syntax.

public boolean renameTo(File dest)

Example

In the following code shows how to use File.renameTo(File dest) method.


/*from  ww  w  .j  ava 2 s  . co m*/
import java.io.File;

public class Main {
  public static void main(String[] args) {

    // create new File objects
    File f = new File("C:/test.txt");
    File f1 = new File("C:/testA.txt");

    // rename file
    boolean bool = f.renameTo(f1);

    // print
    System.out.print("File renamed? " + bool);

  }
}

The code above generates the following result.