我的硬盘中有一个现有文件,我想重命名该文件。
像这样:
new File(path).renameTo(new File(newPath));
You may look at commons-io FileUtils.moveFile . That method tries File.renameTo
, and if that fails, tries to copy&delete the file (eg 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.