Share on Google+Share on Google+

Java file move

In this section, you will learn how to move a file.

Java file move

In this section, you will learn how to move a file.

Description of code:

The package* is based upon streams. Java IO streams provide standardized ways to manipulate file data. Other than read and write, you can perform various file operations.

Basically renameTo() method is used to rename a file but here we have used this method to move a file from one directory to another. You can see in the given example, we have created an object of File class and parse the file with its full path. Now to move this file, we have called renameTo() method through the object of File class. This method consists of another file object, as a parameter, that represents destination path.

Here is the code:


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

Through the renameTo() method, you can move one file from one place to another.


Posted on: April 28, 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.