• Post Reply Bookmark Topic Watch Topic
  • New Topic

Copy folder  RSS feed

 
Rabih Tayyem
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is so simple.... just take a copy of one folder (can contain folders) ex: take a copy of c:\test\ into c:\test1.

I know I can read file by file and move to the destination directory...
but there should be another simple way where you can take a copy of your folder....

THANKS ALL IN ADVANCE
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDK does not contain any methods to copy a File or to copy a folder/directory. (Well, unless some got added in JDK 5.0 - haven't looked in detail yet).

To copy a file, you need to create the new File on disk, open streams on the existing and new file and do a reading/writing loop to copy the data.

To copy a directory, you need to create the new Directory. Then get hold of the list of files in the existing directory. Then use the above method to copy each file.

Jakarta Commons library is a freely-available third-party library that contains utilities to help with this. Look at "Commons IO", and in particular FileUtils and CopyUtils classes. You can copy a file with these, but not a directory, I think.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the java.nio.channels to transfer files without having to go through the hassle of streams. And it's way faster with channels.

Cheers,
Rachel
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!