moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) : Moves a directory to another directory and takes an option to create the new directory or not.moveDirectory(File srcDir, File destDir) – moves a directory to destination and deletes the source directory.The FileUtils class provides many methods to move or rename the files and directories as per requirements. We should be careful that the destination path must be the target path for the file itself not just the new name for the file or the path to the new parent directory.Ĭom.Files.move(originalFile, renamedFile) Ĭom.Files.move(renamedFile, movedFile) 4. This is applicable to renaming and moving, both operations. This Files.move() method moves a file from one path to another. It it is a simple directory renaming in same location in the filesystem, it will succeed.įiles.move(file, file.resolveSibling("demoNew.txt")) įiles.move(file, newDir.resolve(file.getFileName()), Renaming a directory can fail if it requires to move the files in a new location i.e.If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved.If the move cannot be performed as an atomic file system operation (incase of two different filesystems) then AtomicMoveNotSupportedException is thrown. To perform the whole rename or move operation as single atomic operation, we can use StandardCopyOption.ATOMIC_MOVE option.We can use StandardCopyOption.REPLACE_EXISTING in such cases. If target file already exists then move() will fail.If given file and target files are same then this method has no effect.Moving a file will copy the last-modified-time to the target file if supported The move() method moves or renames a file to a target file.The Files.move() is similar to renameTo() except it works with the Path instances instead of File instances. It throws SecurityException if there are write access problems with the old or the new file.įile originalFile = new File("c:/temp/demo.txt") įile renamedFile = new File("c:/temp/demoNew.txt") įile movedFile = new File("c:/temp/moved/demoNew.txt") īoolean isCopied = originalFile.renameTo(renamedFile) īoolean isMoved = renamedFile.renameTo(movedFile).The renameTo() returns true or false denoting if the renaming succeeded or not.doc extensions, type: ren *.txt *.As the method name suggests, renameTo() renames the file to the new name or moves the file to a new directory location. txt file name extensions in the current directory to. If filename2 matches an existing file name, the following message appears: Duplicate file name or file not found. You also can't use this command to rename files across drives or to move files to a different directory.Ĭharacters represented by wildcard characters in filename2 will be identical to the corresponding characters in filename1.įilename2 must be a unique file name. You can't specify a new drive or path when renaming files. You can use wildcard characters to specify new names for multiple files. Filename1 can include wildcard characters ( * and ?). Specifies the location and name of the file or set of files you want to rename. This command is the same as the rename command.