How do I move a file to a directory?
Author: Deron Eriksson
Description: This Java tutorial describes how to move a file to a directory using Commons IO.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
The moveFileToDirectory() method of the FileUtils class of the ApacheSW Commons IOS library can be used to move a file to a directory. It takes the source file as its first argument. It takes the destination directory as its second argument. It takes a boolean as its third argument, which specifies if the destination directory should be created if it doesn't already exist. The MoveFileToDirectoryTest class demonstrates this. MoveFileToDirectoryTest.javapackage test; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class MoveFileToDirectoryTest { public static void main(String[] args) throws IOException { File sourceFile = new File("directory-source/test1.txt"); File destinationDir = new File("directory-destination"); FileUtils.moveFileToDirectory(sourceFile, destinationDir, true); } } Before MoveFileToDirectoryTest is run, we can see that the test1.txt file exists in the directory-source directory. After MoveFileToDirectoryTest is executed, we can see that the directory-destination directory has been created and that the test1.txt file has been moved to this directory. The directory-source directory is now empty. Related Tutorials: |