【Android】简单实现拷贝文件和文件夹到另一个目录下
程序员文章站
2024-03-09 09:56:47
...
转载请注明出处,原文链接:https://blog.csdn.net/u013642500/article/details/80067680
本方法使用前提是已拥有权限,未对权限不足情况进行处理,如有需要可自行添加。
关于读写权限的总结请参考:https://blog.csdn.net/u010784887/article/details/53560025
/**
* 复制单个文件
*
* @param oldPath$Name String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt
* @param newPath$Name String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt
* @return <code>true</code> if and only if the file was copied;
* <code>false</code> otherwise
*/
public boolean copyFile(String oldPath$Name, String newPath$Name) {
try {
File oldFile = new File(oldPath$Name);
if (!oldFile.exists()) {
Log.e("--Method--", "copyFile: oldFile not exist.");
return false;
} else if (!oldFile.isFile()) {
Log.e("--Method--", "copyFile: oldFile not file.");
return false;
} else if (!oldFile.canRead()) {
Log.e("--Method--", "copyFile: oldFile cannot read.");
return false;
}
/* 如果不需要打log,可以使用下面的语句
if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) {
return false;
}
*/
FileInputStream fileInputStream = new FileInputStream(oldPath$Name);
FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
byte[] buffer = new byte[1024];
int byteRead;
while (-1 != (byteRead = fileInputStream.read(buffer))) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 复制文件夹及其中的文件
*
* @param oldPath String 原文件夹路径 如:data/user/0/com.test/files
* @param newPath String 复制后的路径 如:data/user/0/com.test/cache
* @return <code>true</code> if and only if the directory and files were copied;
* <code>false</code> otherwise
*/
public boolean copyFolder(String oldPath, String newPath) {
try {
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!newFile.mkdirs()) {
Log.e("--Method--", "copyFolder: cannot create directory.");
return false;
}
}
File oldFile = new File(oldPath);
String[] files = oldFile.list();
File temp;
for (String file : files) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file);
} else {
temp = new File(oldPath + File.separator + file);
}
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (!temp.exists()) {
Log.e("--Method--", "copyFolder: oldFile not exist.");
return false;
} else if (!temp.isFile()) {
Log.e("--Method--", "copyFolder: oldFile not file.");
return false;
} else if (!temp.canRead()) {
Log.e("--Method--", "copyFolder: oldFile cannot read.");
return false;
} else {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
/* 如果不需要打log,可以使用下面的语句
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (temp.exists() && temp.isFile() && temp.canRead()) {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
*/
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * 复制单个文件 * * @param oldPath$Name String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt * @param newPath$Name String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt * @return <code>true</code> if and only if the file was copied; * <code>false</code> otherwise */ public boolean copyFile(String oldPath$Name, String newPath$Name) { try { File oldFile = new File(oldPath$Name); if (!oldFile.exists()) { Log.e("--Method--", "copyFile: oldFile not exist."); return false; } else if (!oldFile.isFile()) { Log.e("--Method--", "copyFile: oldFile not file."); return false; } else if (!oldFile.canRead()) { Log.e("--Method--", "copyFile: oldFile cannot read."); return false; } /* 如果不需要打log,可以使用下面的语句 if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) { return false; } */ FileInputStream fileInputStream = new FileInputStream(oldPath$Name); //读入原文件 FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name); byte[] buffer = new byte[1024]; int byteRead; while ((byteRead = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 复制文件夹及其中的文件 * * @param oldPath String 原文件夹路径 如:data/user/0/com.test/files * @param newPath String 复制后的路径 如:data/user/0/com.test/cache * @return <code>true</code> if and only if the directory and files were copied; * <code>false</code> otherwise */ public boolean copyFolder(String oldPath, String newPath) { try { File newFile = new File(newPath); if (!newFile.exists()) { if (!newFile.mkdirs()) { Log.e("--Method--", "copyFolder: cannot create directory."); return false; } } File oldFile = new File(oldPath); String[] files = oldFile.list(); File temp; for (String file : files) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file); } else { temp = new File(oldPath + File.separator + file); } if (temp.isDirectory()) { //如果是子文件夹 copyFolder(oldPath + "/" + file, newPath + "/" + file); } else if (!temp.exists()) { Log.e("--Method--", "copyFolder: oldFile not exist."); return false; } else if (!temp.isFile()) { Log.e("--Method--", "copyFolder: oldFile not file."); return false; } else if (!temp.canRead()) { Log.e("--Method--", "copyFolder: oldFile cannot read."); return false; } else { FileInputStream fileInputStream = new FileInputStream(temp); FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName()); byte[] buffer = new byte[1024]; int byteRead; while ((byteRead = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); } /* 如果不需要打log,可以使用下面的语句 if (temp.isDirectory()) { //如果是子文件夹 copyFolder(oldPath + "/" + file, newPath + "/" + file); } else if (temp.exists() && temp.isFile() && temp.canRead()) { FileInputStream fileInputStream = new FileInputStream(temp); FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName()); byte[] buffer = new byte[1024]; int byteRead; while ((byteRead = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); } */ } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
缺陷:复制单文件时(copyFile),无法判断新文件夹是否存在、新文件是否能否写入。
由于本人安卓知识及技术有限,代码如有错误或不足请评论指出,非常感谢!
感谢伊茨米可的原创博客,本文参考链接:https://blog.csdn.net/etzmico/article/details/7786525/
上一篇: c++实现移动文件夹到指定文件夹
下一篇: 复制文件夹及目录下的所有文件