• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Copy folder  RSS feed

 
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
 
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.
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!