public static boolean copyFile(final String srcFilePath,
final String destFilePath) {
return copyOrMoveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
}
private static boolean copyOrMoveFile(final File srcFile, final File destFile) {
if (srcFile == null || destFile == null) return false;
// srcFile equals destFile then return false
if (srcFile.equals(destFile)) return false;
// srcFile doesn't exist or isn't a file then return false
if (!srcFile.exists() || !srcFile.isFile()) return false;
if (destFile.exists()) {
// require delete the old file
if (!destFile.delete()) {// unsuccessfully delete then return false
return false;
}
}
if (!createOrExistsDir(destFile.getParentFile())) return false;
try {
return writeFileFromIS(destFile, new FileInputStream(srcFile), false);
} catch (Exception e) {
logger.e(e);
return false;
}
}
public static boolean writeFileFromIS(final File file,
final InputStream is,
final boolean append) {
if (!createOrExistsFile(file) || is == null) return false;
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file, append));
byte data[] = new byte[8192];
int len;
while ((len = is.read(data, 0, 8192)) != -1) {
os.write(data, 0, len);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Android 复制文件
猜你喜欢
转载自blog.csdn.net/wl724120268/article/details/80527680
今日推荐
周排行