Skip to content

Instantly share code, notes, and snippets.

@sarkerrabi
Created December 22, 2019 05:04
Show Gist options
  • Save sarkerrabi/5cd2cfeae8949717a44753bc19306128 to your computer and use it in GitHub Desktop.
Save sarkerrabi/5cd2cfeae8949717a44753bc19306128 to your computer and use it in GitHub Desktop.
Very common need to use for file management issue
public class CommonFileManagement(){
//delete folder in android
public static void deleteFolderWithContent(String dirName){
File dir = new File(Environment.getExternalStorageDirectory().toString() + "/"+dirName);
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
}
//copy file from one to another
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment