Files.copyRecursive?

Alan Bateman Alan.Bateman at oracle.com
Thu Jun 27 01:45:57 PDT 2013


On 26/06/2013 21:12, Philippe Marschall wrote:
> Hi
>
> Something that bothers me with java.nio.file.Files is that there is no
> way to recursively copy folders (e.g. copy a folder into the zip file
> system). This is especially inconvenient as implementing this on your
> own is quite a bit of work, especially if the source and target are
> not on the same file system. Since Java 8 seems to be release of
> convenience methods I was wondering whether it would make sense to add
> such a method. Attached you'll find a version that I use myself that
> I'm sure still has several issues.
>
> Cheers
> Philippe
Recursive operations is something that we do need to get back to at some 
point. The focus in the original API was to get the primitive operations 
right and then combine with walkFileTree to do recursive operations. The 
issue with providing recursive operations in the API is really deciding 
how they should work when there are errors. Various approaches have been 
suggested, including fast fail, providing a means to collect exceptions, 
putting limits on the number of errors, providing a callback or some 
means to allow the application decide whether to continue. So that topic 
needs further consideration as to whether any of these suggestions are 
really needed. If we do recursive operations then I think the most 
important one to get right is a recursive delete. I mention this one 
because it is by far the hardest to get right and do securely when the 
file system is changing.

As regards a copyFileTree or option to the existing copy method then the 
simplest is along the lines of the example in the FileVisitor javadoc, 
that should be sufficient for most needs and will work even if the 
source and target file tree are associated with different file system 
providers. This simple example doesn't of course attempt to 
copy/translate attributes which I think is why there is a lot more code 
in your implementation.

-Alan.


More information about the nio-dev mailing list