File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Renaming a file in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Renaming a file in Java" Watch "Renaming a file in Java" New topic

Renaming a file in Java

Nair Anoop
Ranch Hand

Joined: May 09, 2001
Posts: 68
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,
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
You read it in. You write it out to a new name. You delete the old one.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Nair Anoop
Ranch Hand

Joined: May 09, 2001
Posts: 68
Cindy, thanks for your help. Wonder how I didn't think of it.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
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");

Val SCJP <BR>going for SCJD
I agree. Here's the link:
subject: Renaming a file in Java
It's not a secret anymore!