aspose file tools*
The moose likes Beginning Java and the fly likes reusing File reference not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reusing File reference not working" Watch "reusing File reference not working" New topic
Author

reusing File reference not working

tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Change into That's not that hard, is it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
tarun saha
Greenhorn

Joined: Mar 23, 2010
Posts: 27
Rob Prime wrote:Change into That's not that hard, is it?


Thanks worked.
But i couldn't understand how..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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).
 
 
subject: reusing File reference not working