This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 9291

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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Regarding --> fileObj.renameTo(newFileObj)
Similar Threads
Unable to Create Non-Read-Only Directories...
Illegal Argument exception
Signing applet on the MAC problems
Creating and Writing a file
checking that an excel file is already opened by another application