拷贝data/data/包名/files文件记下所有文件及文件夹到本地sdcard根目录teddyData_files文件夹下...

 

 1     /**
 2      * copy data/data/包名/files 文件夹下的文件到本地sdcard根目录teddyData_files目录下
 3      */
 4     public boolean copyFileToSdcard() {
 5         boolean state = false;
 6         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
 7             File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), File.separator + "teddy_files_data");
 8             TedSDKLog.d(TAG, "sdcardFilePath" + file.getAbsolutePath());
 9 
10             state = copyFolder(context.getFilesDir().getAbsolutePath(), file.getAbsolutePath());
11         }else{
12         }
13         return state;
14     }
15 
16 
17 
18     /**
19      * 复制整个文件夹内容
20      * @param oldPath String 原文件路径 如:c:/fqf
21      * @param newPath String 复制后路径 如:f:/fqf/ff
22      * @return boolean
23      */
24     public boolean copyFolder(String oldPath, String newPath) {
25         boolean state = false;
26         try {
27             File newFile = new File(newPath);
28             if(newFile.exists()){
29                 newFile.delete();
30             }
31             newFile.mkdir(); //如果文件夹不存在 则建立新文件夹
32             File oldFile = new File(oldPath);
33             String[] file = oldFile.list();
34             File temp = null;
35             for (int i = 0; i < file.length; i++) {
36                 if(oldPath.endsWith(File.separator)){
37                     temp = new File(oldPath+file[i]);
38                 }
39                 else{
40                     temp = new File(oldPath+File.separator+file[i]);
41                 }
42 
43                 if(temp.isFile()){
44                     FileInputStream input = new FileInputStream(temp);
45                     FileOutputStream output = new FileOutputStream(newPath + "/" +
46                             (temp.getName()).toString());
47                     byte[] buf = new byte[1024 * 5];
48                     int len;
49                     while ( (len = input.read(buf)) != -1) {
50                         output.write(buf, 0, len);
51                     }
52                     output.flush();
53                     output.close();
54                     input.close();
55                 }
56                 if(temp.isDirectory()){//如果是子文件夹
57                     copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
58                 }
59             }
60             state = true;
61         }
62         catch (Exception e) {
63             if(Debug.DEBUG) {
64                 TedSDKLog.d(TAG, e.getMessage());
65                 TedSDKLog.d(TAG, "复制整个文件夹内容操作出错");
66                 e.printStackTrace();
67             }
68             state = false;
69         }
70         return  state;
71     }

 

转载于:https://www.cnblogs.com/z2qfei/p/7976415.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值