Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reusing File reference not working

 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUT TRAGICALLY myDir IS NOT POINTING TO THE NEW REFERENCE

As the API says, instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

After renaming the directory, which don't you create a new File instance ?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change into That's not that hard, is it?
 
tarun saha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Change into That's not that hard, is it?


Thanks worked.
But i couldn't understand how..
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As was said before, File is immutable. By calling renameTo you don't change that File object - just the physical file. My code simply gives a reference to the File you are renaming to, then overwrites the old reference (that represents the old file location) with the new reference (that represents the new file location).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic