Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copying Directories

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread...

https://coderanch.com/t/400056/java/java/Search-FIle-System-Pattern-Match
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic