一: Androidmanifest.xml 中添加文件操作權限
<!-- 往SDCard寫入數據權限 -->
<!-- 如果你的項目要使用到對sd卡的創建和刪除等操作的時候需要添加該權限 -->
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
二: 拷貝代碼
package com.update.mapdata.fundrive.dataupdate;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
/**
* 複製指定文件夾下的所有文件和嵌套文件,並保持原來的結構不變,
* 以百分比顯示覆制進度,保留兩位小數
* @author zuofeng.site
* @ f1 代表源文件
* @ f2 目標文件
*/
public class CopyFileUtil {
public static void copyDirectoryOrFileToDirectory(File f1,File f2) throws IOException{
List<String> list = getAllFilePath(f1);
int num = list.size();//總文件路徑數目
Log.i("copypath", "File list size: " + num);
double count = 0.0;
for (String s : list) {
Log.i("copypath", "copy file start");
File path = new File(s);
String name = path.getPath();//獲取源文件路徑
String newName=name.replace(f1.getParent(),f2.getPath());//設置複製後的新路徑
File newFile= new File(newName);
if(path.isDirectory()){//空文件夾創建絕對路徑
boolean b = newFile.mkdirs();
if(b){
count++;
double d=count/num;//把已經複製的文件數量作爲顯示進度的參數
}else{
Log.e("copypath", "mkdirs false:" + path.getPath());
}
}
if(path.isFile()){
int i = newName.lastIndexOf("/");
String temppath = newName.substring(0, i);
File ftemp= new File(temppath);
if(!ftemp.exists()){
ftemp.mkdirs();
}
if(!newFile.exists()){
boolean b = newFile.createNewFile();
//目標創建成功,開啓流
if(b){
Log.i("copypath", "Stream start");
boolean result=copyFile(path, newFile);
Log.i("copypath", "Stream end");
if(result){
count++;
double d=count/num;
}else{
Log.e("copypath", "Stream false:" + path.getPath());
}
}
}
}
Log.i("copypath", "copy file end");
}
}
/**
* 創建複製內容的流方法
* @param sourceFile 源文件
* @param targetFile 目標文件
* @return 返回值代表流程序是否複製完成
* @throws IOException 拋出IO傳輸異常
*/
public static boolean copyFile(File sourceFile,File targetFile) throws IOException{
// 新建文件輸入流並對它進行緩衝
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
// 新建文件輸出流並對它進行緩衝
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
// 緩衝數組
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此緩衝的輸出流
outBuff.flush();
//關閉流
inBuff.close();
outBuff.close();
output.close();
input.close();
return true;
}
/**
* 獲取源文件下所有文件的絕對路徑,存放在list中
* @param file 源文件
* @return 返回值代表源文件下所有文件的絕對路徑
*/
public static List<String> getAllFilePath(File file){
List<String> list=new ArrayList<String>();
if(!file.exists()) {
return list;
}
if(file.isDirectory()){//如果文件是目錄
File[] files=file.listFiles();//把目錄下所有目錄和文件存在數組中
if(file.length()>0){//該目錄非空
for (File f : files) {//遍歷數組內所有對象
if(f.isDirectory()){//如果還是目錄
List<String> mlist=getAllFilePath(f);//調用自身方法循環
list.addAll(mlist);//將每一個子目錄文件夾添加到list集合中
}else{//如果遍歷出來是文件
list.add(f.getPath());//將文件路徑存入list集合
}
}
}else{//如果目錄爲空
list.add(file.getPath());//將空目錄路徑添加到list集合
}
}else{//如果傳進來的file對象是文件
list.add(file.getPath());//將文件路徑添加到list集合
}
return list;
}
}
三: 注意事項:
1. 在調試過程中,代碼創建的文件,通過電腦可能無法瀏覽文件(但文件已經存在與調試機器中,重啓調試設備就可以看 到文件)