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

来源:互联网 发布:拼豆转图软件 编辑:程序博客网 时间:2024/06/08 07:56

【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.    } 
0 0