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.
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/
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.
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);