It's not a secret anymore!
The moose likes Beginning Java and the fly likes Copying Directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying Directories" Watch "Copying Directories" New topic

Copying Directories

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
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

Joined: Mar 26, 2002
Posts: 262
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

Fun programming etcetera!
marc weber

Joined: Aug 31, 2004
Posts: 11343

See this thread...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15099

You've asked this question on at least 4 different Java forums on Internet. I've already posted my answer on 2 of them.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
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.
I agree. Here's the link:
subject: Copying Directories
It's not a secret anymore!