Android Tutorial

12.27.2011

Copy and delete file in android



private void copyFile(String pathIn, String pathOut) {
Log.d(TAG, "copyFile start");
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathIn);
out = new FileOutputStream(pathOut);
copy(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
delFile(pathIn);
} catch (Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
Log.d(TAG, "copyFile end");
}

private boolean delFile(String filePath) {
File file = new File(filePath);
boolean deleted = file.delete();
return deleted;
}

/* ------------- File Processes -------------> */
/**
* Copy data from $from to $to
*
* @param $from
*            source
* @param $to
*            destination
* @throws IOException
*/
public static void copy(InputStream $from, OutputStream $to)
throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = $from.read(buffer)) > 0) {
$to.write(buffer, 0, length);
}
$to.flush();
$to.close();
$from.close();
}

No comments:

Post a Comment