How do I move a file?
Author: Deron Eriksson
Description: This Java tutorial describes how to move a file using Commons IO.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)

The moveFile() method of the FileUtils class of the ApacheSW Commons IOS library can be used to move a file from one location to another. It takes the source file as its first argument and the destination file as its second argument. This is demonstrated in the MoveFileTest class.

package test;



public class MoveFileTest {

	public static void main(String[] args) throws IOException {

		File sourceFile = new File("directory-source/test1.txt");
		File destinationFile = new File("directory-destination/test1.txt");

		FileUtils.moveFile(sourceFile, destinationFile);



Before MoveFileTest is run, we can see that the test1.txt file exists in the directory-source directory. The directory-destination directory is empty.

test1.txt in directory-source

After executing MoveFileTest, we can see that test1.txt has been moved from the directory-source directory to the directory-destination directory.

test1.txt in directory-destination