In this tutorial, I am sharing simple program about How to rename a file in Java.

The file name can be renamed in Java by using method called renameTo(File). Which is belongs to class. The method will return boolean value true if file renamed and false if not. also the sample program were attached in the same post.

package com.dineshkrish.files;
public class RenamingFileExample {
public static void main(String[] args) throws IOException {
// Defining the Old File Name Object
File OldFile = new File("c:\\oldName.txt");
// Defining the New File Name Object
File newFile = new File("c:\\newName.txt");
boolean status = OldFile.renameTo(newFile);
if(status) {
System.out.println("File is renamed successfully!!!");
} else {
System.out.println("Not changes");


1. Java API Documentation


No responses yet

Leave a Reply

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