In this example we will show the method to rename a file in Java.
Source Code
package com.beginner.examples;
import java.io.*;
public class RenameFile {
public static void main(String[] args )
{
try {
File path = new File("example.txt");
File newName = new File("test.txt");
path.renameTo(newName); // rename a file
System.out.println("Renamed succesfully!");
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
Output:
Renamed succesfully!