wood burning stoves 2.0*
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
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: 41182
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 java.io.File.renameTo 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: 8843

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: http://aspose.com/file-tools
subject: copy command and renaming
Similar Threads
How to cut a file from one location
Copying a file form one location to another location
copy files
Property File inside the jar
File copy and paste