Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding --> fileObj.renameTo(newFileObj) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding --> fileObj.renameTo(newFileObj)" Watch "Regarding --> fileObj.renameTo(newFileObj)" New topic

Regarding --> fileObj.renameTo(newFileObj)

Sathi Krishna
Ranch Hand

Joined: Oct 21, 2007
Posts: 51

When I Run the below sample code, I am getting exception at runtime

The output I am getting is

Before Rename : E:\Development\DoNBeTheBest\MyDir\MyFile.txt
After Rename : E:\Development\DoNBeTheBest\MyDir\MyFile.txt MyDir\MyFile.txt (The system cannot find the path specified)
at Method)
at svkr.kb.ch06.ListFiles.main(

Why after renaming also txtFile object pointing the old directory?

Thank You

Ankit Garg

Joined: Aug 03, 2008
Posts: 9463

Sathi Krishna wrote:Why after renaming also txtFile object pointing the old directory?

Because the documentation says so

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

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Regarding --> fileObj.renameTo(newFileObj)
It's not a secret anymore!