Java file move


 

Java file move

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

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 java.io* 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:

import java.io.File;

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.

Ads