I’m trying to create a file for my audio recorder, but this file keeps getting a random name. Because this is hard to work with, I want to rename the file after it has been created with a more meaningful name.
However, even though renameTo returns true, the file has not been renamed.
Am I doing something wrong here?
outfile = File.createTempFile(amount + "_alarmsave", ".3gp",
storageDir);
System.out.println("Old file: "+outfile.getAbsolutePath());
File newFile = new File(outfile.getParent(), "alarmsave_" + amount + ".3gp");
System.out.println("new file: "+newFile.getAbsolutePath());
if(outfile.renameTo(newFile)){
System.out.println("Succes! Name changed to: " + outfile.getName());
}else{
System.out.println("failed");
}
The LogCat output:
01-13 18:27:40.264: I/System.out(22913): Old file: /mnt/sdcard/Personal Alarm/13_alarmsave1623959934.3gp
01-13 18:27:40.264: I/System.out(22913): new file: /mnt/sdcard/Personal Alarm/alarmsave_13.3gp
01-13 18:27:40.284: I/System.out(22913): Succes! Name changed to: 13_alarmsave1623959934.3gp
RenameTo renames the actual file, it doesn’t change the
Fileobject. If you called.exists()you’d find the new file exists and the old one doesn’t.This is because the
Fileclass represents abstract paths rather than actual files on a file system. The idea is thatFile.renameTogives a new name to a file system entry at the given path; it does not change the path itself.