This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NIO2 file copy DirectoryNotEmptyException " Watch "NIO2 file copy DirectoryNotEmptyException " New topic
Author

NIO2 file copy DirectoryNotEmptyException

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Oracle docs: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy%28java.nio.file.Path,%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.

OCAJP 7, OCPJP 7
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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: http://aspose.com/file-tools
 
subject: NIO2 file copy DirectoryNotEmptyException
 
Similar Threads
javax.resource.ResourceException: Unable to get managed connection for NeoDataSource
db2java.zip
JBoss & MySQL
Sonar Installation Problem
Copy Files to Remote System from Local using ANT