Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

rename file

 
Manuel Paco
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manuel Paco
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not work.
File does not get renamed.
 
Manuel Paco
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File does not get renamed.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't those file extensions have to be 3 characters?
Not sense pre-Windows 95.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manuel Paco
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic