Lompat ke konten Lompat ke sidebar Lompat ke footer

How To Make Copy Or Cut/Move And Paste File Atau Folder Android Java Programmatically

How to make copy paste file or folder or cut/move file or folder easy


// just to take note of the location sources
Log.v(TAG, "sourceLocation: " + sourceLocation);
Log.v(TAG, "targetLocation: " + targetLocation);
try {
// 1 = move the file, 2 = copy the file
int actionChoice = 2;
// moving the file to another directory
if (actionChoice == 1) {

if (sourceLocation.renameTo(targetLocation)) {
Log.v(TAG, "Move file successful.");
Toast.makeText(getApplicationContext(), "UserUT.ini Detected", Toast.LENGTH_LONG).show();
} else {
Log.v(TAG, "Move file failed.");
Toast.makeText(getApplicationContext(), "Nothing UserUT.ini!", Toast.LENGTH_LONG).show();
}
} else {
// make sure the target file exists
if (sourceLocation.exists()) {
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();
Log.v(TAG, "Copy file successful.");
} else {
Log.v(TAG, "Copy file failed. Source file missing.");
}
}

} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
close