• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reusing File reference not working

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic