Rename a file using Java

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

    This is an easy way to rename a file:

            File oldfile =new File("test.txt");
            File newfile =new File("test1.txt");
    
            if(oldfile.renameTo(newfile)){
                System.out.println("File renamed");
            }else{
                System.out.println("Sorry! the file can't be renamed");
            }
    

提交回复
热议问题