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 java.io.File 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.

RenamingFileExample.java

package com.dineshkrish.files;
import java.io.File;
import java.io.IOException;
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");
}
}
}

References

1. Java API Documentation

Tags:

No responses yet

Leave a Reply

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