Reputation: 71
I have a piece of code where I am retrieving a file from a URL and saving it to temp folder. For my use case, I need the file name to be a specific string say 'filename'.
So far the code I have is,
try (AutoDeletingFile fileToUpload = new AutoDeletingFile(pullFile(fileUrl))) {
fileToUpload.getFile().renameTo(new File(filename));
}
But when I use fileToUpload.getName post rename, it still gives me the original file name. Is there a clean way to get the file pointer of the new file while renaming it?
Upvotes: 0
Views: 131
Reputation: 21979
Because fileToUpload.getFile()
only return value, not the reference. So, to update the actual file, you need to:
File file = fileToUpload.getFile();
file.renameTo(new File(filename));
fileToUpload.setFile(file);
That and inside the setFile()
, you replace File
variable inside fileToUpload
with the new one.
Upvotes: 1