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 copy command and renaming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "copy command and renaming" Watch "copy command and renaming" New topic

copy command and renaming

praveen livingstone

Joined: Apr 13, 2008
Posts: 7
In a java program, how can I copy a file from a location and past it in another location? Also how can I rename an existing file?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42277
Java does not have a command to copy files. You'd need to use a FileInputStream to read the file, and then write it elsewhere using FileOutputStream. Here's an example of how to do that.

As to renaming (and moving) files, that's done with the method.

Ping & DNS - my free Android networking tools app
praveen livingstone

Joined: Apr 13, 2008
Posts: 7
thanks for the reply but the problem is your method would work for text based copying but i want to copy an image file(GIF) from one location to another. Is there any way that i can do this in JAVA?
Joe Ess

Joined: Oct 29, 2001
Posts: 8927

Did you try Ulf's example with a GIF file?

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
praveen livingstone

Joined: Apr 13, 2008
Posts: 7
@Joe Ess : yea i made mistake, posted without trying the code. now i tried it and its working good. thanks

@Ulf Dittmer : thank you so much
I agree. Here's the link:
subject: copy command and renaming