hey guys, i found a strange java behavior
we all know how to create a file reference, an actual file and directory, renaming it and so on. but the renameTo returns a boolean.
now i will show you a problem where an earlier referenced directory is recreated
[code:]
File myDir = new File("New Directory") //created a reference and myDir is pointing to that reference
myDir.mkdir(); //actually created the directory named "New Directory"
//works great
myDir.renameTo(new File("My Dir")) // created a new file reference named "My Dir" and "New Directory" is changed to "My Dir"
// works great again
// BUT TRAGICALLY myDir IS NOT POINTING TO THE NEW REFERENCE
// myDir is still referring to "New Directory"
So till now I created a directory named "New Directory", renamed it to "My Dir" and i find a "My Dir" on my desktop...........cool.
It all works great till now, superb, but the moment i create a sub directory in "My Dir"- Formerly "New Directory", i am able to do that easily.
BUT IT CREATES THE OLD DIRECTORY AGAIN
so i continue the above code:
myDir = new File("My Dir/Inside Dir"); // This line creates the old directory "New Directory" in the desktop
myDir.mkdirs(); // This line actually creates the nested directory.
(After the file runs successfully) Now i have two directories on the desktop 1. My Dir and 2. New Directory. And inside My Dir i have another directory "Inside Dir".
But the New Directory is HAZARDOUS. It is basically because even though i have renamed the directory, the file object-myDir still is not referring to it. Remember the difference between x=x.concat("world") and x.concat("world")? in second case it does the job but it is lost in the very next moment while in the first case it is assigned to the reference variable/ object x.
So what i meant with the above code is that while calling the constructor File at the last-but-one line, it is creating another directory with the old name(Note this is happening before saying mkdirs()). Now it should not even create the directory before mkdir() is used, but in this case it is created the moment i call the file constructor - The FIRST PROBLEM.
SECOND PROBLEM- I am writing a code where i have to dynamically generate temporary folders and rename them accordingly, then i am forced to reuse the file object. So if every time it creates a new directory in huge directory structure, everybody is going to shoot me down.
Please solve this if you can.
Thanks for any help in advance.
Regards.