Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying Directories  RSS feed

 
Donald Nunn
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • 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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • 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,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!