• 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

File renameTo()

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying with file operation.



OUTPUT is


Why is the path and name of the file still the SAME ??



 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It has done its work, just try to find pack1\niky.txt on your file system.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use meaningful titles.
Having a thread titled "File" hardly makes sense.
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No , i find only find pack2\rex. And nothing in pack1
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kedar,

The code
renames the file referenced by variable f1 to the abstract file name of f2. The abstract path names of f1 and f2 are:

f1: pack1\niky.txt
f2: pack2\rex.txt

So after the above code is executed you will observe that the file name of f1 is changed to pack2\rex.txt and therefore you will find rex.txt file created under pack2 dir. So after the code is executed f1 is pointing to a file object which is still not created and f2 is pointing to an actual file on disk.

Hope this clears your doubt.

Thank You
Hemnath
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately , i did not understand. Can you explain that again??

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kedar Nath wrote:Unfortunately , i did not understand. Can you explain that again??



Just remember whatever pointed by f1 on filesystem that is pack1\nicky.txt, is renamed to pack2\rex.txt, and nicky.txt got deleted.

But f1 is just pointing to pack1\nicky.txt, but if you call f1.exists() you will get false, as whatever pointed by f1 on filesystem is deleted.
and f2.isFile() will return true as f1.renameTo(f2) has created pack2\rex.txt that is pointed by f2.

f1 and f2 just a reference of the File object, File Object is just like any java object, the actual file on file system will be created when you call File.createNewFile() method.

File Object has a instance variable that is String path, this path variable just stores address of the files like pack1\nicky.txt, but this pack1\nicky.txt could or could not exist on the file system. You can check that via calling exists() method.
 
Hemnathbabu Kottaiveedu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes why not.

When you execute the renameTo method on the file object the original file(f1 with abs path pack1\niky.txt) is renamed with the abs path of f2(pack2\rex.txt). So the file location is changed and you will find the file created in pack2 directiory.

And the system out statements that you have put at the end of the program clearly shows that f1 is no more a file now and f2 is actually pointing to a file with name rex.txt.

Thanks,
Hemnath
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou guys , Now i understood.
 
I was born with webbed fish toes. This tiny ad is my only friend:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic