Share on Google+Share on Google+

Java file renameTo

Advertisement
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.

Advertisements

Posted on: June 10, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.