Java source code examples

Java source code samples. Java code examples.

Moving a file or directory in Java

* * * * * 1 votes

Moving a file or directory in Java

The renameTo method of file class can be used for moving a file or directory to another destination.


import java.io.File;
/**
 * File class examples
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Create the destination file/directory.
		File destintation = new File("d:\\temp\\dir\\test.txt");

		// Move the file
		boolean status = file.renameTo(destintation);

		System.out.println("Moving status - "+status);
	}
}

Tags: java.io

Discuss This Code