• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Copying Directories

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way in Java to copy the contents of one directory to another. I've tried the listFiles() method and I can capture the contents of a directory, however, I can't seem to find a way to write the contents to another directory.
Thanks,
[ January 13, 2002: Message edited by: Donald Nunn ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK there is no single method to copy a file, let alone a directory.
To copy a file, you can either use File or RandomAccessFile, copying the contents from one instance to another. For File, you can use streams. For RandomAccessFile, you can use read() and write(). I suspect RandomAccessFile may be faster, as it is lower-level.
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are going to have to go through the directory recursively and get a name for each file/directory. Then you will have to lock a FileInputStream onto each with its acoompanying Reader and create any directories the file requires. Then use a FileOutputStream and its appropriate writer to write the files.
HTH,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic