wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes renameTo()? 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 » Beginning Java
Bookmark "renameTo()?" Watch "renameTo()?" New topic
Author

renameTo()?

Pete Dawson
Greenhorn

Joined: Oct 27, 2009
Posts: 7
Hi,

I'm trying to write a simple GUI file renamer using Netbeans 6.8 on Windows. I am having problems getting the renameTo() method to work.

Can anyone see what I am doing wrong?


It did actually work when I ran it using java -jar from the dist directory in a DOS command window and the file was in the same directory. I'm wondering if it is a permissions problem.

thanks

Pete
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What happens if you use the fullpath of the file, instead of just the filename ?


[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This line:

File tempFile = new File(file.getName());

discards all the information about the file's path, keeping only the name. Java will therefore be looking for a file by that name in the current directory. If that's where the file is, then the code will work, but if it's anywhere else, it won't. Just don't do this "tempFile" thing -- use the File object you get from the chooser directly.


[Jess in Action][AskingGoodQuestions]
Pete Dawson
Greenhorn

Joined: Oct 27, 2009
Posts: 7
Thanks, that did it. I can see why now.

Pete
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: renameTo()?
 
Similar Threads
File Deletion, and problems with overwriting instead of adding to file.
Rename Excel File using Java
Help With File Class
about pathname.
Synchronization problem!!!