

So it is recommended to use the Files.move() method in Java NIO as described below. } NOTE: You cannot use the renameTo() method to move directory, even the directory is empty.This method is platform-dependent and is not guaranteed to work in all cases. The following example moves a file from the current directory to another one (also renames it): File sourceFile = new File("CoverPhoto.png") įile destFile = new File("D:/Photo/ProfilePhoto.png") If the path of the destination File points to another directory, the source file will be moved. }If the destination directory exists, the method return false. }As you can see in this example, the renameTo() method returns a boolean value indicating the renaming succeeded (true) or failed (false) - so you should always check its return value.If the destination file exists, the method returns false.The following example renames the directory “test” to “dist” in the current directory: File sourceFile = new File("test") Rename File/Directory Example with renameTo() methodYou can use the renameTo() method to rename a file or directory to a new name which does not exist.The following example renames a file to a new name in the current directory: File sourceFile = new File("Notes.txt") įile destFile = new File("Keynotes.txt") For that, you would use the following syntax: rename-item " current_filename.ext" "new_filename.To rename or move a file/directory in Java, you can use either the renameTo() method of a Fileobject in the old File I/O API, or the Files.move() method in the new Java NIO API.

RELATED: 9 Ways to Open PowerShell in Windows 10įirst, let’s look at renaming a single file. Once there, click File > Open Windows PowerShell, and then click “Open Windows PowerShell.”


The quickest way to open a PowerShell window at your desired location is to first open the folder in File Explorer. While we’ll only scratch the surface of naming your files, you can do some really powerful things, including piping cmdlets together to batch replace characters in a filename. Windows PowerShell is even more powerful and flexible than Command Prompt when it comes to renaming files and folders in a command-line environment. For example, to rename the file “Home Movies.ogv” to “First Birthday.ogv” you would use the following command: ren "Home Movie.ogv" "First Birthday.ogv" Although the quotes aren’t mandatory, they’re required if either of the current or new names has a space in them.
