aspose file tools*
The moose likes Beginning Java and the fly likes rename file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "rename file" Watch "rename file" New topic
Author

rename file

Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
Help please,
What is wrong with that?
File oldFile = new File( "C:/", "A.jpeg");
File newFile = new File( "C:/", "B.jpeg");
boolean success = false;
success = oldFile.renameTo( newFile );
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

I don't know. What's wrong with that?
(Please tell us if the code doesn't compile, or if it doesn't work; and in either case tell us what error message you're seeing. At first glance the code looks fine.)


[Jess in Action][AskingGoodQuestions]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
At first glance the code looks fine, unless this code doesn't reside in a code block aside from a class code block.
Outside of a code block, you cannot do simple variable assignments separated from variable declarations (though, I seem to recall that compound assignments are ok). So, the last line - success = oldFile.renameTo( newFile ); - would cause a compiler error.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
It does not work.
File does not get renamed.
Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
File does not get renamed.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Well, let's see. I'm not a Windows person, so I can't comment on whether Java interprets the "C:\" drive specifier properly as a directory; I'll assume it does. One possibility: the file's name isn't actually A.jpeg, but A.jpg. Don't those file extensions have to be 3 characters? Have a close look at the file and let us know.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Don't those file extensions have to be 3 characters?
Not sense pre-Windows 95.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I'll agree with Ernest that you might want to check to see if the file is named as you are expecting. Try the following:

I tested this code on my system, and it works just fine.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: rename file