Java file renameTo


 

Java file renameTo

This section demonstrates you the use of renameTo method.

This section demonstrates you the use of renameTo method.

Java file renameTo

This section demonstrates you the use of renameTo method.

Description of code:

Java IO provides various useful tools that enables us to manipulate a file easily. By just using its built in methods, you can perform various file operations. The method renameTo() also belongs to this package. If you want to change the name of the file then you can use this method.

In the given example, we have created an object of File class and parse the file. Then we have called the method renameTo() through the object of File class. This method consists of a file object representing destination file,  and rename the specified file with this destination file.

Here is the code:

import java.io.*;

public class FileRename {
	public static void main(String[] args) {
		File f = new File("C:/filename.txt");
		f.renameTo(new File("C:/new.txt"));
	}
}

Through the method renameTo() , you can rename any file or directory. This method belongs to file class.

Ads