Rename a file using Java

前端 未结 14 825
感情败类
感情败类 2020-11-22 07:54

Can we rename a file say test.txt to test1.txt ?

If test1.txt exists will it rename ?

How do I rename it to the alrea

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 08:03

    Files.move(file.toPath(), fileNew.toPath()); 
    

    works, but only when you close (or autoclose) ALL used resources (InputStream, FileOutputStream etc.) I think the same situation with file.renameTo or FileUtils.moveFile.

提交回复
热议问题