Rename file to uppercase in the same directory usi

2020-04-13 07:32发布

问题:

I am trying to rename files in same Windows directory using Java -

Before: -

C:/Temp/abG.txt

After: -

C:/Temp/ABG.TXT

I have tried using file.renameTo but that did not work. Now I am trying to use -

Path source = file.toPath();
Files.move(source, source.resolveSibling(file.getName().toUpperCase()));

Still no luck. Please help.

回答1:

Finally issue is resolved.

It seems file.renameTo works fine on Windows. The path I mentioned was wrong, rectified it and code worked.

I was using path as C:/Temp/Folder it should be C:/Temp/Folder/



回答2:

I haven't tried running but this logically should work.

  String newFilePath = oldFile.getAbsolutePath().replace(oldFile.getName(), oldFile.getName().toUpperCase());
  File newFile = new File(newFilePath);

  try {
    FileUtils.moveFile(oldFile, newFile);
  } catch (IOException e) {
    e.printStackTrace();
  }

Hope this helps.



回答3:

Try the following

    String FILE_PATH = "C:\\Users\\HariBabuM\\Desktop\\file\\modify";
    File oldFile = new File(FILE_PATH, "fileWithCamelCase.txt");
    File newFile = new File(FILE_PATH, oldFile.getName().toLowerCase());

    oldFile.renameTo(newFile);