Rename a file using Java

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

    Renaming the file by moving it to a new name. (FileUtils is from Apache Commons IO lib)

      String newFilePath = oldFile.getAbsolutePath().replace(oldFile.getName(), "") + newName;
      File newFile = new File(newFilePath);
    
      try {
        FileUtils.moveFile(oldFile, newFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    

提交回复
热议问题