How to rename a file in Java

In this tutorial, we are going to see how to rename a file in Java.

Java provides the renameTo() method to rename a file. The return value (true if the file is renamed successfully, false if it fails) should always be checked to ensure that the file is renamed successfully.

How to rename a file in Java

The following example will rename the existing file “aaaa.txt” to “bbbb.txt”.


public class Main 
  public static void main(String[] args)
		File oldF = new File("aaaa.txt");
		File newF = new File("bbbb.txt");
			System.out.println("File was renamed successfully");
			System.out.println("Unable to rename the file");


File was renamed successfully
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *