Rename file to uppercase in the same directory usi

2020-04-13 07:17发布

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.

3条回答
SAY GOODBYE
2楼-- · 2020-04-13 08:01

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/

查看更多
Ridiculous、
3楼-- · 2020-04-13 08:04

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);
查看更多
混吃等死
4楼-- · 2020-04-13 08:22

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.

查看更多
登录 后发表回答