Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Android Security Essentials Live Lessons this week in the Android 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: 3432
    
  12
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: 24183
    
  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]
 
 
subject: File renameto doesn't change the name of the file
 
Similar Threads
file.renameTo() fails
Doubt:Sequence of the output
Saving files in .txt format using JFileChooser.
renaming an uploaded file
Move File from one directory to another