File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes NIO2 file copy DirectoryNotEmptyException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "NIO2 file copy DirectoryNotEmptyException " Watch "NIO2 file copy DirectoryNotEmptyException " New topic

NIO2 file copy DirectoryNotEmptyException

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Oracle docs:,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29

DirectoryNotEmptyException - the REPLACE_EXISTING option is specified but the file cannot be replaced because it is a non-empty directory (optional specific exception)

Please explain this further

/** Code speaks louder than words */
Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

I am aware that a few years have passed. (9) However I would like to renew this questions. As I would like to know what is the workaround to make it possible to write in a folder with already some content in it without emptying it first.
Thanks a lot in advance.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Well, the error above occurs because you're trying to overwrite the directory, and it's not empty. There are two basic solutions, and they want you to explicitly pick one, rather than picking one for you and hoping it's right. The choices are: (a) delete everything in the directory and make a new one, or (b) leave the existing contents as they are, and write new files within the directory. It sounds like you want option (b). To do that, you need to move down one level in the directory hierarchy. Don't try to copy the directory itself. Instead, copy each file within the directory. If tthe file you're copying has the same name as an existing file, you have the additional choice of whether you want to overwrite the existing file, append to it, skip that file, or throw an error. That's up to you, depending on your requirements.
I agree. Here's the link:
subject: NIO2 file copy DirectoryNotEmptyException
It's not a secret anymore!