• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying Directories

 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am trying to copy directories from one place to another in a
platform independent way. You see the directory i have contains some files in it as well as other directories. Basically this directory also has sub-directories.

Usually when i am copying an array of files this is what i usually do



The above code copies an array of files to the destination. The thing now is that i need to copy an array of directories to a destination. The thing with these directories is that contain sub-directories and i am not able to copy the array of directories.

Basically my question is how do i copy directories that contain files and sub-directories to a destination location?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For every File object you process, you need to determine whether it represents a regular file or a directory. To make this decision, use File.isDirectory().

If it's not a directory, you already know what to do. If it is a directory, you'll need to call File.listFiles() to get an array of File objects contained in the directory.

Note that you should probably be writing a recursive method:
1) base case is that File is a regular file
2) recursive case is when File represents a directory
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread...

http://www.coderanch.com/t/400056/java/java/Search-FIle-System-Pattern-Match
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've asked this question on at least 4 different Java forums on Internet. I've already posted my answer on 2 of them.
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
You've asked this question on at least 4 different Java forums on Internet. I've already posted my answer on 2 of them.


It appears that the Sun forum admins just deleted Richard's thread on this topic there after his being flamed by the regulars for cross-posting.

Note to the Bartenders:
This person's name is Richard West, but I suspect that using the name West Richard is a violation of the naming policy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic