Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Renaming a file in Java

 
Nair Anoop
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Is there any method by which I can RENAME a file using Java (not invoking a MSDOS command) ? Please let me know.
Thanks in advance,
Anoop
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You read it in. You write it out to a new name. You delete the old one.
 
Nair Anoop
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy, thanks for your help. Wonder how I didn't think of it.
~Anoop
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might be doing extra work then needed all you need to do is
call method renameTo on File object.
public class Test {
public static void main(String[] args) {
File f = new File("c:/dude.txt");
File newObj = new File("c:/d.txt");
f.renameTo(newObj);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic