Posted By

nialace on 11/20/10


Tagged


Versions (?)

Copy directory


 / Published in: Java
 

  1. private void copyDirectory(File sourceLocation , File targetLocation) throws IOException {
  2.  
  3. if (sourceLocation.isDirectory()) {
  4. if (!targetLocation.exists()) {
  5. targetLocation.mkdir();
  6. }
  7.  
  8. String[] children = sourceLocation.list();
  9. for (int i=0; i<children.length; i++) {
  10. copyDirectory(new File(sourceLocation, children[i]),
  11. new File(targetLocation, children[i]));
  12. }
  13. } else {
  14.  
  15. InputStream in = new FileInputStream(sourceLocation);
  16. OutputStream out = new FileOutputStream(targetLocation);
  17.  
  18. // Copy the bits from instream to outstream
  19. byte[] buf = new byte[1024];
  20. int len;
  21. while ((len = in.read(buf)) > 0) {
  22. out.write(buf, 0, len);
  23. }
  24. in.close();
  25. out.close();
  26. }
  27. }

Report this snippet  

You need to login to post a comment.