File oldFile = new File("old"); if (oldFile.renameTo(new File("new"))){ System.out.println(oldFile.getName());//this prints "old" } I've looked at openJDK source, and there renameTo(File dest) function looks like this:
public class File implements Serializable, Comparable<File> { static private FileSystem fs = FileSystem.getFileSystem(); private String path; ... public boolean renameTo(File dest) { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite(path); security.checkWrite(dest.path); } return fs.rename(this, dest); } ... } So the path variable never gets changed. Why is that so? What would be the right way to use renamed File variable? Currently i do it like this:
File oldFile = new File("/home/blin/misk/old"); File newFile = new File("/home/blin/misk/new"); if (oldFile.renameTo(newFile)){ oldFile=newFile; System.out.println(oldFile.getName());//this prints "new" }