Mega Code Archive

 
Categories / Android / File
 

Copy File and Directory

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; class Util {   public static void copyDirectory(File sourceLocation, File targetLocation)       throws IOException {     if (sourceLocation.isDirectory()) {       if (!targetLocation.exists()) {         targetLocation.mkdirs();       }       String[] children = sourceLocation.list();       for (int i = 0; i < children.length; i++) {         copyDirectory(new File(sourceLocation, children[i]), new File(             targetLocation, children[i]));       }     } else {       copyFile(sourceLocation, targetLocation);     }   }   /**    * @param sourceLocation    * @param targetLocation    * @throws FileNotFoundException    * @throws IOException    */   public static void copyFile(File sourceLocation, File targetLocation)       throws FileNotFoundException, IOException {     InputStream in = new FileInputStream(sourceLocation);     OutputStream out = new FileOutputStream(targetLocation);     // Copy the bits from instream to outstream     byte[] buf = new byte[1024];     int len;     while ((len = in.read(buf)) > 0) {       out.write(buf, 0, len);     }     in.close();     out.close();   }   static public boolean deleteDirectory(File path) {     if (path.exists()) {       File[] files = path.listFiles();       for (int i = 0; i < files.length; i++) {         if (files[i].isDirectory()) {           deleteDirectory(files[i]);         } else {           files[i].delete();         }       }     }     return (path.delete());   } }