I am having weird behavior with Java
File file = new File("test.txt"); file.reName(new File("test1.txt"));
The file is successfully renamed from test.txtto test1.txt, but if I do
test.txt
test1.txt
System.out.println(file.getCanonicalPath()); //This return test.txt
Is this expected? And what is a clean way to solve this problem?
Yes, it is expected. Fileobjects are immutable and just represent the file name.
File
You can think of it this way: an object Fileis a link to the file, not the file itself.
- , , , (.. ). foo1.txt foo1.bak, File, foo1.txt, - FileOutputStream.
foo1.txt
foo1.bak
FileOutputStream
, . File . . renameTo , , . , File.
renameTo