This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying whole directories - odd behaviour  RSS feed

 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll fess up right away.

I don't see a "simple" way of doing it with java.io classes, so here is a small method I'm using:


My directory looks like:
/home/foo/templates/pattern
/home/foo/templates/pattern/images (empty directory)
/home/foo/templates/pattern/reports (empty directory)
/home/foo/templates/pattern/web/1 (that is actually a file '1' with no extension)

I'm trying to copy from
/home/foo/templates/pattern
into
/home/foo/instances/XX/

Where XX is a numbered instance of the pattern directory.

I'm invoking:
Files.copy("/home/foo/templates/pattern", "/home/foo/instances/XX");

What I'm getting is:
/home/foo/instances/XX/patterns/...
instead of
/home/foo/instances/XX/...

Where '...' represents the contents of the patterns directory.

I've also tried:
Files.copy("/home/foo/templates/pattern/*", "/home/foo/instances/XX");

I'm running Slack, so the Files.copy is properly using the unix commands (I've checked with logging statements).
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hrm..

reading man pages every so often might help.

... if /a is a directory, then `cp -r /a /b' will copy /a to /b/a and /a/x to /b/a/x in case a directory /b existed already, but it will copy /a to /b and /a/x to /b/x if there was no /b beforehand, while it will fail in case there was an ordinary file /b.

(I was getting caught by the first case. I was creating my 'XX' directory beforehand).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!