开发者

How to rename an existing file

开发者 https://www.devze.com 2022-12-30 06:37 出处:网络
I have an existing file in my HDD and I want rename this开发者_JAVA百科 file.Like this: new File(path).renameTo(new File(newPath));

I have an existing file in my HDD and I want rename this开发者_JAVA百科 file.


Like this:

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 (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!

0

精彩评论

暂无评论...
验证码 换一张
取 消