• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NIO2 file copy DirectoryNotEmptyException

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4906
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to overwrite whatever is there?
 
Saloon Keeper
Posts: 15608
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

It depends on what you mean by "overwrite whatever is there". Let's say you have two directories:

If you copy source/ to target/, do you want the result to be this:
or this:

In both cases, a.txt would be overwritten. The difference is whether c.txt is retained.
reply
    Bookmark Topic Watch Topic
  • New Topic