Rename a file using Java

前端 未结 14 808
感情败类
感情败类 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

    Try This

    File file=new File("Your File");
    boolean renameResult = file.renameTo(new File("New Name"));
    // todo: check renameResult
    

    Note : We should always check the renameTo return value to make sure rename file is successful because it’s platform dependent(different Operating system, different file system) and it doesn’t throw IO exception if rename fails.

提交回复
热议问题