zoukankan      html  css  js  c++  java
  • Android 文件在SD卡中的拷贝功能

    //源文件路径

    final String FROMPATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/SYIMS";

    //目标文件路径
    final String TOPATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/himi";

    if(copy(FROMPATH, TOPATH)==0){
       Toast.makeText(WebViewActivity.this,"文件拷贝成功!!!",20000).show();
      }else{
       Toast.makeText(WebViewActivity.this,"文件拷贝失败!!!",20000).show();
    }

    public int copy(String fromFile, String toFile){
      //要复制的文件目录
      File[] currentFiles;
      File root = new File(fromFile);
      //如同判断SD卡是否存在或者文件是否存在
      //如果不存在则 return出去
      if(!root.exists()){
       return -1;
      }
      //如果存在则获取当前目录下的全部文件 填充数组
      currentFiles = root.listFiles();
      //目标目录
      File targetDir = new File(toFile);
      //创建目录
      if(!targetDir.exists()){
       targetDir.mkdirs();
      }
      //遍历要复制该目录下的全部文件
      for(int i= 0;i<currentFiles.length;i++){
       if(currentFiles[i].isDirectory()){//如果当前项为子目录 进行递归
       copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");
       }else{//如果当前项为文件则进行文件拷贝
        CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
       }
      }
      return 0;
     }

     //文件拷贝
    //要复制的目录下的所有非子目录(文件夹)文件拷贝

    public int CopySdcardFile(String fromFile, String toFile){
      try {
       InputStream fosfrom = new FileInputStream(fromFile);
       OutputStream fosto = new FileOutputStream(toFile);
       byte bt[] = new byte[1024];
       int c;
       while ((c = fosfrom.read(bt)) > 0) {
        fosto.write(bt, 0, c);
       }
       fosfrom.close();
       fosto.close();
       return 0;
      
      } catch (Exception ex) {
       return -1;
      }
     }

  • 相关阅读:
    shell编程之变量
    linux更换yum源
    windows系统安装jdk并设置环境变量
    linux安装jdk
    mysql中null与“空值”的坑
    mysql服务器3306端口不能远程连接的解决
    Memcached
    redis memcached MongoDB
    postman进行http接口测试
    C# 开发Chrome内核浏览器(WebKit.net)
  • 原文地址:https://www.cnblogs.com/xiao-xu/p/3407487.html
Copyright © 2011-2022 走看看