Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copying folder and all of its contents

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
I came across code to copy a file, simple enough



but does anyone know a similar method that would work for a whole folder and the folder contents?

Regards

Mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty easy to write. Look into File.list and try something like

Now if your folder contains subfolders, you'll have to add a bit

Are you comfortable with that kind of recursion? Is that enough to go on?
[ November 09, 2006: Message edited by: Stan James ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys i forgot to put in the copy method,

but anyway i came across this code for copying a directory which works great



thanks

mark
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks stan,

i like the way you broke it down, makes alot of sense seeing it in that form rather than a bunch of code.

Cheers mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I frequently scribble that kind of psuedo code out to start a problem. What's fun is to translate to code line per line. I haven't compiled or checked this but consider ...

That's pretty near one-to-one with the sketch from before. Your logic is actually better because it allows the first "source" argument to be a file or directory. For fun try to make it this expressive and keep that feature.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why reinvent the wheel? I'd simply use http://jakarta.apache.org/commons/io/api-release/org/apache/commons/io/FileUtils.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic