问题
I have an existing file in my HDD and I want rename this file.
回答1:
Like this:
new File(path).renameTo(new File(newPath));
回答2:
You may look at commons-io FileUtils.moveFile. That method tries File.renameTo, and if that fails, tries to copy&delete the file (e.g. when moving from one drive to another).
If you use File.renameTo
directly (which should suffice if you only want to rename a file in the same directory), make sure you evaluate the returned boolean value!
来源:https://stackoverflow.com/questions/2804105/how-to-rename-an-existing-file