How to rename an existing file

吃可爱长大的小学妹 提交于 2019-12-31 05:20:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!