This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes File renameto doesn't change the name of the file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "File renameto doesn Watch "File renameto doesn New topic
Author

File renameto doesn't change the name of the file

Fabio Landoni
Greenhorn

Joined: Mar 25, 2010
Posts: 3
I have a little problem with the file name.

This is my code:

After the renameTo I check thath "hasBeenRenamed" is true and i hask for a syste.out.println(oldFile.getName()) it gives me the old name of the file, but in the filesystem the file has been renamed!

Some one can help me?
thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
That's how File works.

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


Joanne
Fabio Landoni
Greenhorn

Joined: Mar 25, 2010
Posts: 3
Joanne Neal wrote:That's how File works.

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



Ok thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Remember, a java.io.File doesn't represent a "file", really; it represents a path to a file. When you call methods like renameTo() or size(), they look onto the file system at the corresponding path, and they won't work unless the file exists. But you can create a java.io.File to represent completely nonexistent paths, and that's perfectly OK.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File renameto doesn't change the name of the file