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
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet