|
之前做文件拷贝很多时候都是InputStream读了然后OutputStream去写,偶尔发现FileChannel这个类,发现用它去做看上去像个样子。。。
下面是个代码片段
FileChannel fis = null;
FileChannel fos = null;
try {
fis = new FileInputStream(file).getChannel();
fos = new FileOutputStream(new File(SAVED_PATH + SAVED_DATA)).getChannel();
long result = fis.transferTo(0, fis.size(), fos);
if (result == fis.size()) {
Toast.makeText(ctx, ctx.getString(R.string.msg_export_success) + SAVED_PATH + SAVED_DATA,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ctx, R.string.error_dataformat, Toast.LENGTH_LONG)
.show();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} |
|