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

NIO2 file copy DirectoryNotEmptyException

 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic