Androin学习笔记四十六:拷贝文件到另一个目录下

出处:http://blog.csdn.net/etzmico/article/details/7786525

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * 复制单个文件  
  3.      * @param oldPath String 原文件路径 如:c:/fqf.txt  
  4.      * @param newPath String 复制后路径 如:f:/fqf.txt  
  5.      * @return boolean  
  6.      */   
  7.    public void copyFile(String oldPath, String newPath) {   
  8.        try {   
  9.            int bytesum = 0;   
  10.            int byteread = 0;   
  11.            File oldfile = new File(oldPath);   
  12.            if (oldfile.exists()) { //文件存在时   
  13.                InputStream inStream = new FileInputStream(oldPath); //读入原文件   
  14.                FileOutputStream fs = new FileOutputStream(newPath);   
  15.                byte[] buffer = new byte[1444];   
  16.                int length;   
  17.                while ( (byteread = inStream.read(buffer)) != -1) {   
  18.                    bytesum += byteread; //字节数 文件大小   
  19.                    System.out.println(bytesum);   
  20.                    fs.write(buffer, 0, byteread);   
  21.                }   
  22.                inStream.close();   
  23.            }   
  24.        }   
  25.        catch (Exception e) {   
  26.            System.out.println("复制单个文件操作出错");   
  27.            e.printStackTrace();   
  28.   
  29.        }   
  30.   
  31.    }   
  32.   
  33.    /**  
  34.      * 复制整个文件夹内容  
  35.      * @param oldPath String 原文件路径 如:c:/fqf  
  36.      * @param newPath String 复制后路径 如:f:/fqf/ff  
  37.      * @return boolean  
  38.      */   
  39.    public void copyFolder(String oldPath, String newPath) {   
  40.   
  41.        try {   
  42.            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹   
  43.            File a=new File(oldPath);   
  44.            String[] file=a.list();   
  45.            File temp=null;   
  46.            for (int i = 0; i < file.length; i++) {   
  47.                if(oldPath.endsWith(File.separator)){   
  48.                    temp=new File(oldPath+file[i]);   
  49.                }   
  50.                else{   
  51.                    temp=new File(oldPath+File.separator+file[i]);   
  52.                }   
  53.   
  54.                if(temp.isFile()){   
  55.                    FileInputStream input = new FileInputStream(temp);   
  56.                    FileOutputStream output = new FileOutputStream(newPath + "/" +   
  57.                            (temp.getName()).toString());   
  58.                    byte[] b = new byte[1024 * 5];   
  59.                    int len;   
  60.                    while ( (len = input.read(b)) != -1) {   
  61.                        output.write(b, 0, len);   
  62.                    }   
  63.                    output.flush();   
  64.                    output.close();   
  65.                    input.close();   
  66.                }   
  67.                if(temp.isDirectory()){//如果是子文件夹   
  68.                    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);   
  69.                }   
  70.            }   
  71.        }   
  72.        catch (Exception e) {   
  73.            System.out.println("复制整个文件夹内容操作出错");   
  74.            e.printStackTrace();   
  75.   
  76.        }   
  77.   
  78.    }  



PS:

拷贝assets目录下文件

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. InputStream is = ctx.getAssets().open("test.apk");    


特别感谢jqj1107提的建议,写代码时要谨慎,尽可能不使用try/catch,拷贝文件时检查文件属性等参数,确保万无一失

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if (!oldfile.exists()) {return ;}  
  2. if (!oldfile.isFile()) {return ;}  
  3. if (!oldfile.canRead()) {return ;}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值