【Android】拷贝文件到另一个目录下

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

 

【Android】拷贝文件到另一个目录下

 

  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.    } 
/** 
     * 复制单个文件 
     * @param oldPath String 原文件路径 如:c:/fqf.txt 
     * @param newPath String 复制后路径 如:f:/fqf.txt 
     * @return boolean 
     */ 
   public void copyFile(String oldPath, String newPath) { 
       try { 
           int bytesum = 0; 
           int byteread = 0; 
           File oldfile = new File(oldPath); 
           if (!oldfile.exists()) { //文件不存在时 
               InputStream inStream = new FileInputStream(oldPath); //读入原文件 
               FileOutputStream fs = new FileOutputStream(newPath); 
               byte[] buffer = new byte[1444]; 
               int length; 
               while ( (byteread = inStream.read(buffer)) != -1) { 
                   bytesum += byteread; //字节数 文件大小 
                   System.out.println(bytesum); 
                   fs.write(buffer, 0, byteread); 
               } 
               inStream.close(); 
           } 
       } 
       catch (Exception e) { 
           System.out.println("复制单个文件操作出错"); 
           e.printStackTrace(); 

       } 

   } 

   /** 
     * 复制整个文件夹内容 
     * @param oldPath String 原文件路径 如:c:/fqf 
     * @param newPath String 复制后路径 如:f:/fqf/ff 
     * @return boolean 
     */ 
   public void copyFolder(String oldPath, String newPath) { 

       try { 
           (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
           File a=new File(oldPath); 
           String[] file=a.list(); 
           File temp=null; 
           for (int i = 0; i < file.length; i++) { 
               if(oldPath.endsWith(File.separator)){ 
                   temp=new File(oldPath+file[i]); 
               } 
               else{ 
                   temp=new File(oldPath+File.separator+file[i]); 
               } 

               if(temp.isFile()){ 
                   FileInputStream input = new FileInputStream(temp); 
                   FileOutputStream output = new FileOutputStream(newPath + "/" + 
                           (temp.getName()).toString()); 
                   byte[] b = new byte[1024 * 5]; 
                   int len; 
                   while ( (len = input.read(b)) != -1) { 
                       output.write(b, 0, len); 
                   } 
                   output.flush(); 
                   output.close(); 
                   input.close(); 
               } 
               if(temp.isDirectory()){//如果是子文件夹 
                   copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
               } 
           } 
       } 
       catch (Exception e) { 
           System.out.println("复制整个文件夹内容操作出错"); 
           e.printStackTrace(); 

       } 

   }



PS:

拷贝assets目录下文件

  1. InputStream is = ctx.getAssets().open("test.apk");   
InputStream is = ctx.getAssets().open("test.apk");  


注意:要有对应的权限。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值