80,355
社区成员
发帖
与我相关
我的任务
分享
android递归复制文件夹
//方法
/**
* 复制一个目录及其子目录、文件到另外一个目录
* @param src
* @param dest
* @throws IOException
*/
private void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
//引用
File src=new File("/data/logs");
File dest=new File("/sdcard/datalogs");
try {
copyFolder(src,dest);
Log.i("wp----------------->","sucess");
} catch (IOException e) {
e.printStackTrace();
Log.i("wp----------------->","error"+e);
}